Thread: Problems with strcat Char Array when element is 0 not \0 (NULL terminated)

  1. #1
    Registered User
    Join Date
    Oct 2009

    Smile 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:

    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",            
                    // 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 
                    printf("Initial N. of Versions:             %X :   %X  :  %X : %X  :  %X :  %X  :  %X : %X  :  %X 
                   // 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 
                    // 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),*
                    // 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),*
                    printf("The String Lenght of ptr is: %d", strlen(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!

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    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

  3. #3
    Registered User
    Join Date
    Oct 2009
    Yeah , I guess I should work with a complete string already...

    I will try it this week and I will post what I found.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

Tags for this Thread