(NULL terminated)
This is a discussion on Problems with strcat Char Array when element is 0 not (NULL terminated) within the
C Programming forums, part of the General Programming Boards category; Hi, I'm new at the Forum, just I Hope to find some help here
I have some troubles when concatenating ...
-
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!
-
Registered User
I believe '\0' is the same as 0 which is the same as NULL.
In any case, just don't use strcat. You are not using valid characters, which are characters from 1-255 (0 is special for ending strings). Simply you cannot use 0 as a value for strings, only to end the string.
If you want to concate a string with 0-255 values then you have no ending character. Thus you will have to insert the length of the array on your homemade strcat
-
Yeah , I guess I should work with a complete string already...
I will try it this week and I will post what I found.
Cheers!
Popular pages Recent additions
Similar Threads
-
By Nexus-ZERO in forum C Programming
Replies: 24
Last Post: 12-17-2008, 04:48 PM
-
By Nephiroth in forum Windows Programming
Replies: 8
Last Post: 03-12-2006, 05:23 AM
-
By Prelude in forum A Brief History of Cprogramming.com
Replies: 16
Last Post: 10-02-2004, 03:00 PM
-
By Prelude in forum A Brief History of Cprogramming.com
Replies: 15
Last Post: 01-02-2004, 09:33 AM
-
By SAMSAM in forum Game Programming
Replies: 1
Last Post: 03-03-2003, 06:48 PM
Tags for this Thread