I am using two uint8_t pointers
uint8_t *A_string = "hello"
uint8_t *B_string = "world"
And I am trying to concatenate both these using strcat
strcat(A_string, B_string);
I am getting an error saying "uint8_t is incompatible with parameter of type char* restrict"
So I type casted both A_string and B_string to char* and tried . Now I am not getting the error but the concatenation is not happening .
Can anyone please let me know how do I convatenate two strings which are of type uint8_t * ?
A_string
points to an unmodifiable string literal. However, the first argument ofstrcat
mustn’t be a pointer to a string literal — it must be a pointer to a modifiable array, and furthermore the array must be large enough to hold the result of the concatenation.To fix the issue, declare
A_string
as an array of sufficient size.Furthermore, heed the compiler warnings: you have a potential signedness mismatch in your code that might cause problems. In fact, you probably want to use
char
instead ofuint8_t
here.Here’s a fixed version:
In reality you usually wouldn’t hard-code the array size since if you knew it you could just as well concatenate the string literals yourself in the source code.
Instead you’d need to calculate the requires size, and dynamically allocate a buffer of sufficient size using
malloc
: