Like Tree1Likes
  • 1 Post By laserlight

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

This is a discussion on An Issue about size of string[] and *string, and assigning to each other within the C Programming forums, part of the General Programming Boards category; Recently, I compiled the following program, and it ran accurately. But when I manipulated the code in the program, even ...

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

    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?
    Name:  build situtation.png
Views: 201
Size:  12.3 KB




    Note: PROGRAM 1, PROGRAM 2 and PROGRAM 3 is output in this way:
    Name:  output.png
Views: 151
Size:  7.3 KB



    PROGRAM 4 is output in this way:
    Name:  output 2.png
Views: 139
Size:  17.6 KB
    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
    21,931
    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.
    Salem likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    48
    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-20-2008, 11:15 PM
  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, 05:18 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21