Thread: Entering data into array of char pointers

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    Entering data into array of char pointers

    Hi,
    I'm trying to enter the days of the week into this array of char pointers, but the output is strange.
    Code:
    Please enter the days of the week:
    
    
    Day 1: Monday
    
    Day 2: tuesday
    
    Day 3: Wednsday
    
    Day 4: Thurday
    
    Day 5: Friday
    
    Day 6: Saturday
    
    Day 7: Sunday
    
    The days of the week are:
    
    
    MondtuesWednThurFridSatuSunday
    tuesWednThurFridSatuSunday
    WednThurFridSatuSunday
    ThurFridSatuSunday
    FridSatuSunday
    SatuSunday
    Sunday
    Is this somehow a problem with the \0 not getting added to each element?
    Code:
    #include <stdio.h>
    
    main()
    
    {
          char * days[7];
          int day;
          
          printf("\nPlease enter the days of the week: \n\n");
          for(day=0; day<7; ++day)
          {
          printf("\nDay %d: " , day+1);
          scanf("\n%s" , &days[day]);
          }
          
          printf("\nThe days of the week are: \n\n");
          for(day=0; day<7; ++day)
          {
          printf("\n%s" , &days[day]);
          
          }
    }
    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    It works great if you only enter 1, 2 or 3 characters in for each day. But as soon as you enter four or more characters things go haywire.

    Interesting.

    EDIT: Here you go...make it a multi-dimensional array:

    Code:
    #include <stdio.h>
    
    main()
    {
          char *days[7][20];
          int day;
          
          printf("\nPlease enter the days of the week: \n\n");
          for(day=0; day<7; ++day)
          {
            printf("\nDay %d: ", day+1);
            scanf("\n%s", &days[day]);
          }
          
          printf("\nThe days of the week are: \n\n");
          for(day=0; day<7; ++day)
            printf("\n%s", &days[day]);
                
          fflush(stdin);
          getchar();
                
    }
    Last edited by TCB; 04-08-2006 at 11:24 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have an array of pointers. So what did you make them point it? Nothing. Or rather, you didn't make them point anywhere. They just point to some random spot in memory. You're lucky your program runs at all.

    [edit]
    No. That's wrong too. All you have now is a two dimensional array of pointers to characters. They still don't point to any actual characters.
    [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    Code:
    #include <stdio.h>
    
    main()
    {
          char days[7][20];
          int day;
          
          printf("\nPlease enter the days of the week: \n\n");
          for(day=0; day<7; ++day)
          {
            printf("\nDay %d: ", day+1);
            scanf("\n%s", days[day]);
          }
          
          printf("\nThe days of the week are: \n\n");
          for(day=0; day<7; ++day)
            printf("\n%s", days[day]);
                
          fflush(stdin);
          getchar();
                
    }

    LOL...acutally I was just looking at that as you posted! So I re-wrote it (cause I didn't understand why it needed a pointer) and it works just fine without them.

    TB

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    I'm just initializing the data into the pointers for now, so why wouldn't their contents print correctly?

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    Well, I am just learning C myself and do not know much at all about pointers. But I do know a little bit about char arrays, and just figured out that the problem with your code had something to do with the fact that your first array was the day, but you didn't specify the number of characters each day could hold.

    So when the input was 3 chars or less, it worked fine. Go over 3 chars and it didn't work. Is the default array size 5 bytes, I wonder?

    3 chars, a '\n', and the null...

    TB

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    I was really just trying this to find out if when you enter data into an array of pointers if the \0 (NULL) would get added automatically. I'm thinking it doesn't.

    Im just starting to learn pointers, and when you do an array of them you don't have to specify the number of characters they will hold, they will make the space exactly the correct size. This is what makes them a more efficient choice than the multidimensional array. Your multidimensional array will allocate 20 spaces, but if the names entered are on average only 8 thats alot of wasted space, so the array of pointers allocates exactly enough space for the length of the word.

    So does anyone know if the null characters get added by default when you do this?
    Last edited by richdb; 04-08-2006 at 11:42 PM.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Nope, the NULL terminator you see at the end of strings are actually hand-code-added in the *scanf(), *gets(), str*() functions etc. They're added manually in the CStdLib.

    Remember, a pointer is, to the compiler, just another block of memory. What you want to do with the pointer is what you do with the pointer -- no more, no less.





    when you do an array of them you don't have to specify the number of characters they will hold, they will make the space exactly the correct size
    WRONG. Pointers are just a 32-bit address number. No memory is allocated. If you want dynamic allocation, use realloc().
    Last edited by jafet; 04-09-2006 at 12:06 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jafet
    WRONG. Pointers are just a 32-bit address number.
    There's nothing in the standard that denotes the size of a pointer. Nothing says it has to be "just a 32-bit address".


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    So my screwy output is not because of missing '\0's?
    I'm trying to get each word on its own line.

    I'm really just trying to see if you can manually enter the data into an array of pointers and have it work as if you initialized it like this:
    Code:
    char * array[3] = {"Monday", "Tuesday", "Wednsday"};
    It seems that you can't.
    Last edited by richdb; 04-09-2006 at 12:34 AM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > fflush(stdin);
    This is undefined - see the FAQ.

    > So when the input was 3 chars or less, it worked fine
    No it didn't. Don't confuse "seems to work" with being correct.
    Small programs in particular have the annoying property of actually doing what was expected even despite gross programming mistakes. In a larger program, the same mistake would just kill the program in it's tracks.

    These are the warnings for your first program - consider using these flags when compiling your code.
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c:5: warning: return type defaults to ‘int’
    foo.c: In function ‘main’:
    foo.c:13: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    foo.c:19: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    foo.c:22: warning: control reaches end of non-void function
    All you were doing was using the actual pointer itself to store a string, and since you seem to have 4-byte pointers, you could store 3 chars and a \0 without trashing any memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    So I would have to make the pointers large enough to store the words?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > So I would have to make the pointers large enough to store the words?
    You can't in advance of reading in the string.

    Normally I do
    char buff[BUFSIZ];
    fgets( buff, sizeof buff, stdin );

    Then I can
    - call strlen() to work out the true length of the string I need to store
    - call malloc to allocate that much space for the string and it's \0
    - call strcpy to safely store the string.
    - re-use buff[] for the next input.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    Salem,

    Re: fflush(stdin);

    I got that from the C-programming course I have been taking; all the projects seem to use the fflush() function to flush the newline character prior to using scanf() to read in some data.

    I read the tutorial on it in the FAQ and found it interesting, though I am not sure what to use in it's place. So I will research it a bit more today.

    TB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM