473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

pointers to char

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.
Nov 15 '05 #1
3 1320
gordy <go***@cwazy.co.uk> 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) ks***@mib.org <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.
Nov 15 '05 #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:Th*************@gmail.com>
Nov 15 '05 #3
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.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
19
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
1
by: ketema | last post by:
Hello, I was wondering if someone could help me with a function I am trying to write. The purpose of the function is to read in text from a file in the following format: FIRSTNAME LASTNAME...
11
by: Seven Kast USA | last post by:
hi What is use of pointerrs in c programming.is fasster than other types by KAST
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
38
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers...
13
by: arnuld | last post by:
at the very beginning of the chapter, i see some statements i am unable to understand. i know the "Pointer" takes the address of a variable, useful if, in case, we want to manipulate that variable...
8
by: Piotrek | last post by:
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas...
33
by: pateldm15 | last post by:
How do I sort an string array using pointers
14
by: stevenruiz | last post by:
Hello All My question mainly is how to use/reference Double Pointers? I am currently trying to understand what the meaning of a 'vector of pointers' means also? What I am trying to do is take...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.