Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointers to char

Reply
Thread Tools

pointers to char

 
 
gordy
Guest
Posts: n/a
 
      10-15-2005
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.
 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      10-15-2005
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: <(E-Mail Removed)>


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-15-2005
gordy <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
gordy
Guest
Posts: n/a
 
      10-15-2005
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Pointers to char pointers in functions newbie C Programming 9 09-24-2006 10:31 AM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments