Thread: An Issue about size of string[] and *string, and assigning to each other

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52

    Question An Issue about size of string[] and *string, and assigning to each other

    Recently, I compiled the following program, and it ran accurately. But when I manipulated the code in the program, even if I compiled without problem that it doesn't run properly.

    PROGRAM 1: Here is the unmodified code, and it's running properly;
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void copy1(char* const, const char* const);
    void copy2(char*      , const char*);
    
    
    int main()
    {
        char string1[40];
        char *string2 = "Bendimi cigner asarim!"; // 22 literal character
        char string3[40];
        char string4[] = "Hangi yamyam bana cilgin gibi bakicakmis, sasarim!"; // 50 literal character
    
    
        copy1(string1,string2);
        printf("%s\n",string1);
    
    
        copy2(string3,string4);
        printf("%s\n",string3);
    
    
        return 0;
    }
    
    
    void copy1(char *const s1, const char *const s2)
    {
        int i;
    
    
        for (i=0; (s1[i]=s2[i]) != '\0'; i++) {
            ;
        }
    
    
    }
    
    
    void copy2(char *s3, const char *s4)
    {
        for(  ; (*s3=*s4) != '\0'; s3++,s4++) {
            ;                   
        }
    }

    PROGRAM 2: Here is the modified code, and it's running properly;
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void copy1(char* const, const char* const);
    void copy2(char*      , const char*);
    
    
    int main()
    {
        char string1[40];
        char string2[] = "Bendimi cigner asarim!"; // LOOK AT NOTATION IN THIS LINE
        char string3[40];
        char *string4 = "Hangi yamyam bana cilgin gibi bakicakmis, sasarim!"; // AND THIS LINE...
    
    
        copy1(string1,string2);
        printf("%s\n",string1);
    
    
        copy2(string3,string4);
        printf("%s\n",string3);
    
    
        return 0;
    }
    
    
    void copy1(char *const s1, const char *const s2)
    {
        int i;
    
    
        for (i=0; (s1[i]=s2[i]) != '\0'; i++) {
            ;
        }
    
    
    }
    
    
    void copy2(char *s3, const char *s4)
    {
        for(  ; (*s3=*s4) != '\0'; s3++,s4++) {
            ;                   
        }
    }

    PROGRAM 3: Here is the modified code, and it's running properly;
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void copy1(char* const, const char* const);
    void copy2(char*      , const char*);
    
    
    int main()
    {
        char string1[10]; // LOOK AT SIZE
        char *string2 = "Bendimi cigner asarim!"; 
        char string3[10];// AND THIS...
        char string4[] = "Hangi yamyam bana cilgin gibi bakicakmis, sasarim!"; 
    
    
        copy1(string1,string2);
        printf("%s\n",string1);
    
    
        copy2(string3,string4);
        printf("%s\n",string3);
    
    
        return 0;
    }
    
    
    void copy1(char *const s1, const char *const s2)
    {
        int i;
    
    
        for (i=0; (s1[i]=s2[i]) != '\0'; i++) {
            ;
        }
    
    
    }
    
    
    void copy2(char *s3, const char *s4)
    {
        for(  ; (*s3=*s4) != '\0'; s3++,s4++) {
            ;                   
        }
    }

    Program 4: Here is the modified program, and it is not running properly!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void copy1(char* const, const char* const);
    void copy2(char*      , const char*);
    
    
    int main()
    {
        char string1[10]; // LOOK AT SIZE
        char string2[] = "Bendimi cigner asarim!"; // LOOK AT NOTATION
        char string3[10];// SIZE...
        char *string4 = "Hangi yamyam bana cilgin gibi bakicakmis, sasarim!"; // LOOK AT NOTATION
    
    
        copy1(string1,string2);
        printf("%s\n",string1);
    
    
        copy2(string3,string4);
        printf("%s\n",string3);
    
    
        return 0;
    }
    
    
    void copy1(char *const s1, const char *const s2)
    {
        int i;
    
    
        for (i=0; (s1[i]=s2[i]) != '\0'; i++) {
            ;
        }
    
    
    }
    
    
    void copy2(char *s3, const char *s4)
    {
        for(  ; (*s3=*s4) != '\0'; s3++,s4++) {
            ;                   
        }
    }
    Questions:
    1. string2 has 22 literal characters and string4 has 50 literal characters, doesn't they?
    2. How string4 which has 50 literal characters can copy entirely to string3 in the PROGRAM 1 when string3 can take 40 literal characters as maximum? Why do the compiler give an error about this issue, and run properly?
    3. How string2 which has 22 literal characters can copy entirely to string1 int he PROGRAM 3 when string 1 can take 10 literal characters as maximum? Why do the compiler give an error about this issue, and run properly?
    4. Seeing that the compiler runs the codes(in PROGRAM 1,2,3) properly, ignoring the sizes of strings,well, why the PROGRAM 4 do not run properly? Why the compiler give a warning or an error for PROGRAM 4, although it can run even if it execute the problematic?
    An Issue about size of string[] and *string, and assigning to each other-build-situtation-png




    Note: PROGRAM 1, PROGRAM 2 and PROGRAM 3 is output in this way:
    An Issue about size of string[] and *string, and assigning to each other-output-png



    PROGRAM 4 is output in this way:
    An Issue about size of string[] and *string, and assigning to each other-output-2-png
    Last edited by hefese; 08-05-2012 at 10:07 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hefese
    1. string2 has 22 literal characters and string4 has 50 literal characters, doesn't they?
    string2 has a length of 22, but it consists of 23 characters, including the null character. string4 has a length of 50, but it consists of 51 characters, including the null character.

    Quote Originally Posted by hefese
    2. How string4 which has 50 literal characters can copy entirely to string3 in the PROGRAM 1 when string3 can take 40 literal characters as maximum? Why do the compiler give an error about this issue, and run properly?
    You have a buffer overflow, resulting in undefined behaviour. One characteristic of undefined behaviour is that the compiler is not required to detect it and report an error. Another characteristic of undefined behaviour is that the program may appear to run correctly, but it could also run incorrectly or even crash.

    Quote Originally Posted by hefese
    3. How string2 which has 22 literal characters can copy entirely to string1 int he PROGRAM 3 when string 1 can take 10 literal characters as maximum? Why do the compiler give an error about this issue, and run properly?
    Answered for #1.

    Quote Originally Posted by hefese
    4. Seeing that the compiler runs the codes(in PROGRAM 1,2,3) properly, ignoring the sizes of strings,well, why the PROGRAM 4 do not run properly? Why the compiler give a warning or an error for PROGRAM 4, although it can run even if it execute the problematic?
    Answered for #2. Note that it is a run time error, not a compile error.

    Basically, you should always ensure that the array to which you are copying a string has enough space to store the string, including the null character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52
    I thought that I didn't understand the compiler sometimes gave an error, sometimes didn't give an error, because there was an relationship which I haven't known yet between array(string[]) and pointer(*string), and also their size. But, obviously you are right. It's just about spacing to store the string, including the null character and other situtation is about buffering overflow. Thank you so much for giving a reply, reading my long text.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assigning ints into a string
    By shintaro in forum C++ Programming
    Replies: 7
    Last Post: 11-21-2008, 12:15 AM
  2. assigning a string
    By steve1_rm in forum C Programming
    Replies: 5
    Last Post: 04-14-2008, 02:40 AM
  3. assigning a string to a variable
    By xp5 in forum C Programming
    Replies: 12
    Last Post: 08-30-2007, 01:13 AM
  4. Assigning string to values
    By Itanium in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2003, 07:28 AM
  5. assigning string problem
    By sjfx in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2002, 06:18 PM

Tags for this Thread