pointers to char

Discussion in 'C Programming' started by gordy, Oct 15, 2005.

  1. gordy

    gordy Guest

    Newbie question, please by gentle.

    I'm trying to read in a text file containing the days of the week, one on
    each line. Each line should be pointed to by an array of pointers to char
    but I'm ending up with each pointer pointing to the same string.

    #include <stdio.h>
    #include <stdlib.h>

    #define DAYS 7

    int main()
    {
    int count = 0, bytes_read;
    char *strings[DAYS], *buffer = NULL;
    FILE *fp;
    size_t size = 1;

    if((fp = fopen("days", "r")) == NULL)
    {
    puts("File not found");
    exit(EXIT_FAILURE);
    }

    while(bytes_read = (getline(&buffer ,&size, fp)) != -1)
    strings[count++] = buffer;

    while(count--)
    printf("\t\t%s", *(strings + count));

    fclose(fp);
    return 0;
    }

    file days:
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday

    I'm sure I'm missing something silly here, any help would be appreciated.

    gordy.
     
    gordy, Oct 15, 2005
    #1
    1. Advertising

  2. gordy wrote:
    >
    > Newbie question, please by gentle.
    >
    > I'm trying to read in a text file containing the days of the week, one on
    > each line. Each line should be pointed to by an array of pointers to char
    > but I'm ending up with each pointer pointing to the same string.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > #define DAYS 7
    >
    > int main()
    > {
    > int count = 0, bytes_read;
    > char *strings[DAYS], *buffer = NULL;
    > FILE *fp;
    > size_t size = 1;
    >
    > if((fp = fopen("days", "r")) == NULL)
    > {
    > puts("File not found");
    > exit(EXIT_FAILURE);
    > }
    >
    > while(bytes_read = (getline(&buffer ,&size, fp)) != -1)


    I don't know what getline() does. While my system has a getline()
    function, it is totally different that yours, as mine is prototyped
    as:

    char *getline(char *prompt);

    I am guessing, based on context, that you pass it:

    The address of a char* for the buffer. If the pointer is
    NULL, then a buffer will be allocated for you.

    The address of an int, to return the size of the buffer.

    The FILE* stream to read from.

    And it returns the number of bytes read.

    Continuing with my assumptions, if buffer is not NULL, then it
    will use the buffer/length which is passed to it.

    > strings[count++] = buffer;


    If my assumptions above are true, then once the buffer is allocated
    for the first call, it will continue using the same buffer, as your
    "char *buffer" is no longer NULL.

    >
    > while(count--)
    > printf("\t\t%s", *(strings + count));


    Is ther any reason you don't use "strings[count]" here? It's much
    clearer, at least for me.

    >
    > fclose(fp);
    > return 0;
    > }
    >
    > file days:
    > Monday
    > Tuesday
    > Wednesday
    > Thursday
    > Friday
    > Saturday
    > Sunday
    >
    > I'm sure I'm missing something silly here, any help would be appreciated.


    If any of my assumptions above are wrong, then you are going to have to
    describe how your getline() function works.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Oct 15, 2005
    #2
    1. Advertising

  3. gordy <> writes:
    > Newbie question, please by gentle.
    >
    > I'm trying to read in a text file containing the days of the week, one on
    > each line. Each line should be pointed to by an array of pointers to char
    > but I'm ending up with each pointer pointing to the same string.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > #define DAYS 7
    >
    > int main()
    > {
    > int count = 0, bytes_read;
    > char *strings[DAYS], *buffer = NULL;
    > FILE *fp;
    > size_t size = 1;
    >
    > if((fp = fopen("days", "r")) == NULL)
    > {
    > puts("File not found");
    > exit(EXIT_FAILURE);
    > }
    >
    > while(bytes_read = (getline(&buffer ,&size, fp)) != -1)


    getline() is a non-standard function.

    > strings[count++] = buffer;


    This is a pointer assignment. You pass its address to getline(), but
    unless getline() changes the value you're assigning the same pointer
    value to each element of strings.

    You probably want to copy the string using strcpy() -- which means
    you also need to allocate memory using malloc() (and don't forget
    to check whether malloc() succeeded).

    > while(count--)
    > printf("\t\t%s", *(strings + count));


    The expression *(strings + count) is better written as strings[count].

    > fclose(fp);
    > return 0;
    > }


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 15, 2005
    #3
  4. gordy

    gordy Guest

    On Sat, 15 Oct 2005 15:16:09 -0400, Kenneth Brody wrote:

    > gordy wrote:
    >>
    >> Newbie question, please by gentle.
    >>
    >> I'm trying to read in a text file containing the days of the week, one on
    >> each line. Each line should be pointed to by an array of pointers to char
    >> but I'm ending up with each pointer pointing to the same string.
    >>
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >>
    >> #define DAYS 7
    >>
    >> int main()
    >> {
    >> int count = 0, bytes_read;
    >> char *strings[DAYS], *buffer = NULL;
    >> FILE *fp;
    >> size_t size = 1;
    >>
    >> if((fp = fopen("days", "r")) == NULL)
    >> {
    >> puts("File not found");
    >> exit(EXIT_FAILURE);
    >> }
    >>
    >> while(bytes_read = (getline(&buffer ,&size, fp)) != -1)

    >
    > I don't know what getline() does. While my system has a getline()
    > function, it is totally different that yours, as mine is prototyped
    > as:
    >
    > char *getline(char *prompt);
    >
    > I am guessing, based on context, that you pass it:
    >
    > The address of a char* for the buffer. If the pointer is
    > NULL, then a buffer will be allocated for you.
    >
    > The address of an int, to return the size of the buffer.
    >
    > The FILE* stream to read from.
    >
    > And it returns the number of bytes read.
    >
    > Continuing with my assumptions, if buffer is not NULL, then it
    > will use the buffer/length which is passed to it.
    >
    >> strings[count++] = buffer;

    >
    > If my assumptions above are true, then once the buffer is allocated
    > for the first call, it will continue using the same buffer, as your
    > "char *buffer" is no longer NULL.


    following this advice I changed the code to:

    while(bytes_read = (getline(&buffer ,&size, fp)) != -1)
    {
    strings[count++] = buffer;
    buffer = NULL;
    }
    >
    >>
    >> while(count--)
    >> printf("\t\t%s", *(strings + count));

    and:
    printf("\t\t%s", strings[count]);

    >
    > Is ther any reason you don't use "strings[count]" here? It's much
    > clearer, at least for me.
    >
    >>
    >> fclose(fp);
    >> return 0;
    >> }
    >>
    >> file days:
    >> Monday
    >> Tuesday
    >> Wednesday
    >> Thursday
    >> Friday
    >> Saturday
    >> Sunday
    >>
    >> I'm sure I'm missing something silly here, any help would be appreciated.

    >
    > If any of my assumptions above are wrong, then you are going to have to
    > describe how your getline() function works.


    Your assumptions were spot on, it's now working as I wanted it to.

    many thanks for your help.
     
    gordy, Oct 15, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    587
  2. wwj
    Replies:
    24
    Views:
    2,557
    Mike Wahler
    Nov 7, 2003
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,123
    Ian Collins
    May 9, 2006
  4. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    323
    August Karlstrom
    Sep 24, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    710
Loading...

Share This Page