Problems with strcat Char Array when element is 0 not \0 (NULL terminated)
Hi, I'm new at the Forum, just I Hope to find some help here :)
I have some troubles when concatenating three char arrays. In fact, what I'm trying to do is the following:
Code:
int main(int argc, char** argv) {
static char type[] = { 0x00,0x01,'\0'}; // I already tryied integers instead of hex values
static char number_of_versions[] = {0x03,'\0'};
static char versions[] = {0x04,0x05,'\0'};
char *ptr;
// When I print this the result is: 3
// I tryied to allocate some memory as I want to save the result string in ptr.
// See that I added +1 at the final for the \0 null terminated character of the string
ptr = (char *)malloc(sizeof(type) + sizeof(number_of_versions)+sizeof(versions)+1);
// Some printf to be sure that Im not missing anything. By the way, if I use strlen,
// the result is 0. Even if I inizialize string as {0,1,'\0'}.
printf ("Sizeof Type %d, Sizeof number_of_versions %d, Sizeof versions %d \n",
sizeof(type),sizeof(number_of_versions),sizeof(versions));
// To see what's in memory, even after some steps forwards.
printf("Initial type[] is: %x : %x : %x \n",*type,*(type+1),*(type+2));
printf("Initial ptr is: %X : %X : %X : %X : %X : %X : %X : %X : %X
\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5),*(ptr+6),*(ptr+7),*(ptr+8));
printf("Initial N. of Versions: %X : %X : %X : %X : %X : %X : %X : %X : %X
\n",*number_of_versions,*(number_of_versions+1),*(number_of_versions+2),*
(number_of_versions+3),*(number_of_versions+4),*(number_of_versions+5),*
(number_of_versions+6),*(number_of_versions+7),*(number_of_versions+8));
// I do ptr=type because if I try with strcpy, It will not take in account the string as the first
// element of type is 0 and it takes it as a NULL terminated character, so Its wrong :(
ptr = type;
printf("After ptr=type, ptr is: %X : %X : %X : %X : %X : %X : %X : %X : %X
\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5),*(ptr+6),*(ptr+7),*(ptr+8));
// Now ptr is good, it is array 0,1 but when it concatenates it with number_of_versions,
// it doesn't take in account the ptr string as the first value is zero, so same problem,
// and the result is just ptr = {3, '\0'} in place of {0,1,3,'\0'};
strcat (ptr, number_of_versions);
printf("After first concatenation ptr is: %X : %X : %X : %X : %X : %X : %X : %X :
%X \n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5),*(ptr+6),*(ptr+7),*(ptr+8));
printf("After first concatenation number_of_versions is: %X : %X : %X : %X : %X :
%X : %X : %X : %X \n",*number_of_versions,*(number_of_versions+1),*
(number_of_versions+2),*(number_of_versions+3),*(number_of_versions+4),*
(number_of_versions+5),*(number_of_versions+6),*(number_of_versions+7),*
(number_of_versions+8));
// Finaly I want to concatenate this last string (versions),
strcat (ptr, versions);
printf("After second concatenation, ptr is: %X : %X : %X : %X : %X : %X : %X :
%X : %X: %X : %X : %X : %X \n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5),*
(ptr+6),*(ptr+7),*(ptr+8),*(ptr+9),*(ptr+10),*(ptr+11),*(ptr+12));
printf("The String Lenght of ptr is: %d", strlen(ptr));
free(ptr);
return 0;
}
So, how Do I concatenate strings which contain zero values and do not represent it as zerro values as NULL characters?? Thank you!