3
votes

I am currently trying to use strcat on two unsigned char but I keep getting a warning. Here is what I have for strcat:

unsigned char A[100],B[100];
A[0] = '1';
B[0] = '2';
strcat(A,B);

I am trying to put them togther so that I get A as the combination of both of them. Any suggestions?

5
Post the warning you are getting?Karthikeyan.R.S
Do you need to initialise the rest of the array? Also should you assign the result to something?jnd
Add A[1] = B[1] = '\0'; then it will work. strcat assumes a few things. Also make sure to cast the arguments.Mohit Jain
How to ask a better question: change I keep getting a warning to the specific warning message.Yu Hao
strcat only stops when it reachs a NUL string termination character, both for the source string and for the destination string. Neither of the arrays 'A[]' nor 'B[]' currently have a string NUL termination character (in this case at A[1] and B[1], so strcat will keep looking for the end of the A[] string, even if that string is not within the bounds of the A[] array. Then it will append the characters in the B[] array until is encounters a NUL termination char even if that character is not within the bounds of the B[] array. I.E. this is undefined behaviour leading to a seg fault eventuser3629249

5 Answers

4
votes
strcpy(A,"1");
strcpy(B,"2");
strcat((char *)A,(char *)B);

You are just initilizing the first character in the array and not the char array

Else you can

char A[100] = "";
char B[100] = ""; 

and later you can have

char A[0] = '1';
char B[0] = '2';

strcat() protoype is

char *strcat(char *dest, const char *src)

So the passed type should be char *

3
votes

The warning is because you are passing unsigned char* into a function that is meant for char*.

If you want to copy unsigned char, you should be using memcpy.

#include <string.h>

int main(){
    unsigned char A[100],B[100];
    A[0] =  '1';
    B[0] =  '2';
    memcpy(A + 1, B, 1);
    return 0;
}
1
votes

You could do something like this

#include <stdio.h>

unsigned char* ustrcat(unsigned char *dest, const unsigned char *src) {
  unsigned int length_1 = 0;
  unsigned int length_2 = 0;
  // find the end of the first unsigned char string
  for (; dest[length_1] != '\0'; length_1++);
  //input unsigned chars into the string from that position to the end
  for (; src[length_2] != '\0'; length_2++)
    dest[length_1+length_2] = src[length_2];
  // place a NULL terminating character at the end
  dest[length_1+length_2] = '\0';
  return dest;
}

int main(){
  unsigned char A[100] = "",B[100] = "";
  A[0] = '1';
  B[0] = '2';
  ustrcat(A,B);
  printf("%s\n", A);
  return 0;
}

Basically I just rewrote the strcat function for unsigned char type

0
votes

char, signed char and unsigned char are three separate types. That means you can't pass an unsigned char * to a function expecting a char * without a cast. Either change to simple char:

char A[100], B[100];

or add casts in your strcat() call:

strcat((char *)A, (char *)B);

Also - don't forget to null-terminate those strings!

A[1] = 0;
B[1] = 0;
-1
votes
unsigned char A[100]={0}, B[100]={0};  // initialize the array to end the strings

A[0] = '1';                            // initialize the two strings
B[0] = '2';

strcat(A,B);                           // and simply copy them