Couldn't spot the error.

This is a discussion on Couldn't spot the error. within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> main() { char *c[3]; int count; for( count = 0; count < 3 ; count++) { printf("Enter your ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Question Couldn't spot the error.

    #include <stdio.h>

    main()
    {
    char *c[3];
    int count;

    for( count = 0; count < 3 ; count++)
    {
    printf("Enter your %d c: ", count + 1);
    scanf("%s", c[count]);
    }

    for( count = 0 ; count < 3; count++)
    printf("%s\n", c[count]);

    return 0;
    }

    In this code, I tried to store each string into their respective array of pointers but the problem is when I type one string and press enter, error window pop up. Assumption: Memory is enough. Please help.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You need to allocate memory for the strings.

    char c[3][STRING_LENGTH];

    Or with malloc or such thing. You now don't have memory allocated for the strings.

  3. #3
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    This should work for you:

    Code:
    #include <stdio.h> 
    
    #define MAX 100
    main() { 
      char *c[3][MAX]; 
      int count; 
    
      for(count = 0; count < 3 ; count++) { 
        printf("Enter your %d c: ", count + 1); 
        scanf("%s", c[count]); 
      } 
    
      for(count = 0 ; count < 3; count++) 
        printf("%s\n", c[count]); 
    
      return 0; 
    }

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Almost right, but

    char *c [3][MAX];

    should be

    char c [3][MAX];

    And just a maintenance hint. Your program is better maintanable when you substitute the 3's in your program by a constant. When the number of strings changes, you only need to do that at one place in the code. In larger code it's easy to forget one replacement.

    Code:
    #include <stdio.h> 
    
    #define MAX                     100
    #define NR_OF_STRINGS  3
    
    int main (void) 
    { 
        char c [NR_OF_STRINGS][MAX]; 
        int count; 
    
        for (count = 0; count < NR_OF_STRINGS ; count++) 
        { 
            printf("Enter your %d c: ", count + 1); 
            scanf("%s", c[count]); 
        } 
    
        for(count = 0 ; count < NR_OF_STRINGS; count++) 
            printf("%s\n", c[count]); 
    
        return 0; 
    }

  5. #5
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    While your right about the constant, the program will still work with c as a pointer. It's just because of the way the pointer is being used that it doesn't make sense to keep it as such.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >While your right about the constant, the program will still work
    >with c as a pointer. It's just because of the way the pointer is
    >being used that it doesn't make sense to keep it as such.

    Hmmm. I don't get it.

    If I have

    char *c [3][MAX];

    then I can allocate memory for a 3D array, just the way like

    char c [MAX_X][MAX_Y][MAX_Z];

    and to me this is quite different from

    char *c [3];

    which can be used to allocate memory for a 2D array, just like

    char c [MAX_X][MAX_Y];

    I agree that c can still be used as a pointer to a string, but in my opinion it is unnecessary. To me it seems it's just like using

    char **c;

    for keeping a string.

  7. #7
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Yah I agree, its a definate waste. I can't even figure out how to make an array of pointers to strings work.

    Like:

    Code:
    #include <stdio.h> 
    
    #define MAX 100
    #define NR_OF_STRINGS 3
    
    main() { 
      char *c[NR_OF_STRINGS]; 
      char temp[MAX];
      int count; 
    
      for(count = 0; count < NR_OF_STRINGS ; count++) { 
        printf("Enter your %d c: ", count); 
        gets(temp);
        printf("c[%d] = %s\n", count, temp);
        c[count] = temp;
      } 
    
      //c[0] = "first.";
      //c[1] = "second.";
      //c[2] = "third.";
    
      for(count = 0 ; count < NR_OF_STRINGS; count++) 
        printf("%s\n", c[count]); 
    
      return 0; 
    }
    This will output the last string you put in three times. However if you did:

    Code:
      c[0] = "first.";
      c[1] = "second.";
      c[2] = "third.";
    it outputs fine.



    heh

    I know im doing something wrong but, eh, everything ive looked up thus far hasn't indicated what it is.

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    This

    c[count] = temp;

    is why it fails. You have an array, called temp. And you set all the three pointers, c[0], c[1] and c[2] to temp. So after the loop the situation is as follows:

    c[0] points to temp
    c[1] points to temp
    c[2] points to temp

    So all pointers point to the same string. The string where temp points to is the last entered string. That's why you get the same string 3 times. It's just printing the string where temp points to 3 times.

  9. #9
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    return type unespecified

    did u especify the return type to; void or int:

    int main(){
    ...
    }

    void main(){
    ...
    }

    that's a lil i can do now the others already answered to most of your bugs.
    ‹nicode¨>world = 10.0£

  10. #10
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    I think I get it, when you do:

    Code:
      c[0] = "first.";
      c[1] = "second.";
      c[2] = "third.";
    You are saying the pointer c[0] will point to some array not really defined which has "first" in it. c[1] is basicaly a whole nother array, which has "second" in it. etc.

    If you do:

    Code:
      for(count = 0; count < NR_OF_STRINGS ; count++) { 
        printf("Enter your %d c: ", count); 
        gets(temp);
        printf("c[%d] = %s\n", count, temp);
        c[count] = temp;
      }
    You are not having c control the "making" of the array, you are telling it to point to temp, which changes every new loop through. If you made temp a two dimensional array and had it increment each time along with the pointer it would work. But this would be a waste of energy for what this person seems to be trying to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM

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