Re: pointers to char



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.


.



Relevant Pages

  • EXPLOIT Re: Pavuk Digest Authentication Buffer Overflow
    ... char *method; ... * the auth_digest pointer, the user pointer, and the buf pointer. ... * used as the strings that get printed into buffer. ... int conn ...
    (Bugtraq)
  • [net-next PATCH 4/5] igb: Add support for enabling VFs to PF driver.
    ... pointer to the hardware struct ... under the terms and conditions of the GNU General Public License, ... * @msg: The message buffer ... * returns SUCCESS if it successfully copied message into the buffer ...
    (Linux-Kernel)
  • Re: Array of struct & segmentation fault :-(
    ... trying to assign a pointer = pointer like this: ... int counter; ... we will add new buffer at the end of this ... You should initialize the 'bufferlist' array in main, ...
    (comp.lang.c)
  • Re: void** and c_f_pointer
    ... Look like the confusion arises from the concept of C's pointer. ... of the client buffer or its value. ... int *i; ... SUBROUTINE write_buffer ...
    (comp.lang.fortran)
  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)