iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
change this:
*line++; //point *line to the next array element
to this:
line++; //point *line to the next array element
line is the pointer. *line is the value at the address being held in the pointer. You want to advance the pointer to the next element in array, not the value of the next element of the array.
As an aside, it seems to me, there is a logic problem here. That is, once you have used line to put the characters in the array how do you get back to where you started so you can use the information you entered? I suspect the author of the book doesn't care about that for now. Their goal in this example was likely to demonstrate some of the properties of arrays and pointers and they will address the logic error in this example down the road.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
void ReadLine( char *line ) // line is a string
{
while ( (*line = getchar() ) != '\n' )
line++;
*line = 0;
}
This is a buffer overflow waiting to happen. What happens if someone passes an array of some arbitrary size, say 50 bytes, and the user enters 51 bytes? The function should really take a second argument that specifies the size of the string, and then it should stop reading characters if it reaches that mark.
> That is, once you have used line to put the characters in the array how do you get back to where you started so you can use the information you entered?
That shouldn't be a problem here, since the pointer local to the ReadLine function is changed, and the calling function's pointer/array is not.
GloriousEremite
Junior Poster in Training
65 posts since Jul 2006
Reputation Points: 108
Solved Threads: 14
change this:
*line++; //point *line to the next array element
to this:
line++; //point *line to the next array element
line is the pointer. *line is the value at the address being held in the pointer. You want to advance the pointer to the next element in array, not the value of the next element of the array.
It isn't incrementing the value -- it is evaluating the current value (and discarding the result) and then advancing the pointer.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Maybe I should post ALL of the code before you/we indict this guy. I swear this book was great unitl page 172. :lol:
Well, when authors haven't covered basic user input by the time they are having you (the user) enter input, I'd say [bad things].
And when they do stuff to make it uncompileable for folks trying to play along, it's a shortcut they need to explain better:
#include <c.h> //This is to bring in the define of true
(We may not have this -- which is coded to look like a standard header, but it isn't. So it's really not ALL of the code.):?: another question: Why does the program wait for the user to enter the data? If you uncomment the two printf()s that I inserted you'll see what I'm talking about.Your printf is asking to wait for input:
void ReadLine( char *line )
{
//printf("moopants\n");
//printf("%d",getchar() );
while ( (*line = getchar()) != '\n' )
{
line++;
}
*line = kZeroByte;
}
But then again, without the header I can't compile it. So I can't build it. So I can't run it.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314