944,082 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2612
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 16th, 2006
0

[noob] introduction+strange loops.

Expand Post »
Introduction
hey all, I'm matt. I'm new to this forum and C. I hope to stick around and eventually be able to ANSWER questions instead of sit around being new to this.

but for now, new to this is what I am.

My Background
I've been learning from Dave Mark's "Learn C on a Macintosh" I have basic knowledge of java and full knowledge of JavaScript. My home turf is max/msp.

The Confusing Example
Everything was going fine until I hit this one function in the examples. now I'm completely confused.

  1. void ReadLine( char *line ) // line is a string
  2. {
  3. while ( (*line = getchar() ) != '\n' )
  4. line++;
  5.  
  6. *line = 0;
  7. }

The Questions
Can someone tell me, step by step what this code does???
It's so compact that I really just don't understand it.
How does getchar know which char to get?
How can you assign and compare in the same expression?
Why increment a char?
all these questions and more, answered when you stay tuned to
"When Geeks Pull Out Their Remaining Hair"
after these messages....


-matt
Reputation Points: 10
Solved Threads: 0
Newbie Poster
puuukeey is offline Offline
4 posts
since Aug 2006
Aug 16th, 2006
0

Re: [noob] introduction+strange loops.

It's not very good example especially for beginners.
You need to know that C strings are ordinary arrays of characters that ends with zero character '\0' or just 0.
for example if you define c string like:
  1. char text[50];
It means it can store 49 character + zero character.
You can look to name of an array as a pointer to first element in an array (although it's not quite true).
You have a function that receive char pointer and that is just it a pointer to character. It can be incremented and decremented.
If you call function EeadLine(text) that means that pinter to the first character in array text is copied and thransfered to function. You can increment and decrement pointer in that function, so you can traverse your array.
getchar() function read from standard input and returns int, not char, and in your example there is truncation which is why I don't like this example very much, but this can serve to teach you more about C strings.
Line:
  1. *line = getchar()
means this:
read character and place it in location where line points to i.e. change element on which line points with element that getchar read from input.
Every time you enter some text in console window and hit Enter a newline marker '\n' is placed in text.
So in loop you basicaly read one character at a time an store it on place to which line points to, next increment line (since this is pointer) to point to the next location, place another element and so on until you reach '\n' which denotes end of line. Last step is to place zero character '\0' or 0 to end string.
I hope this explanation helps.
Last edited by Micko; Aug 16th, 2006 at 4:36 am.
Reputation Points: 55
Solved Threads: 6
Junior Poster
Micko is offline Offline
148 posts
since Aug 2005
Aug 16th, 2006
0

Re: [noob] introduction+strange loops.

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 16th, 2006
0

Re: [noob] introduction+strange loops.

thanks so much for your reply. I'm processing this info so let me confirm my findings.

**first of all, when evaluating an expression like so,
  1. (a=b) != c
  2. // pseudo code
first, b is assigned to a
then b=a evaluates to b ?
so the full expression returns b !=c ?


**Second, the equivalent would be (and I know this is bad form)

  1. void ReadLine( char *line )
  2. {
  3. while(true)
  4. {
  5. //transfer elements of buffer to array
  6. //beginning with array[0]
  7. *line=getchar();
  8.  
  9. if(*line=='\n') //if you encounter a \n break out of the loop
  10. {break;}
  11.  
  12. *line++; //point *line to the next array element
  13. }
  14.  
  15. *line = 0; //put a 0 at the end of the array to make it a string
  16. }
Last edited by puuukeey; Aug 16th, 2006 at 11:52 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
puuukeey is offline Offline
4 posts
since Aug 2006
Aug 16th, 2006
0

Re: [noob] introduction+strange loops.

Quote originally posted by puuukeey ...
thanks so much for your reply. I'm processing this info so let me confirm my findings.

**first of all, when evaluating an expression like so,
  1. (a=b) != c
  2. // pseudo code
first, b is assigned to a
then b=a evaluates to b ?
so the full expression returns b !=c ?
You're right...
Expression returns 1 or 0 depending of result of comparision.
  1. #include <stdio.h>
  2.  
  3. int main (void)
  4. {
  5. int a = 10, b = 12, c = 15;
  6.  
  7. printf ("%d", ( (a = b) != c ) );
  8. return 0;
  9. }
Last edited by Micko; Aug 16th, 2006 at 4:45 pm.
Reputation Points: 55
Solved Threads: 6
Junior Poster
Micko is offline Offline
148 posts
since Aug 2005
Aug 16th, 2006
0

Re: [noob] introduction+strange loops.

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Aug 16th, 2006
0

Re: [noob] introduction+strange loops.

Awesome. Learning Is Fun! Thank You!!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
puuukeey is offline Offline
4 posts
since Aug 2006
Aug 16th, 2006
0

Re: [noob] introduction+strange loops.

  1. void ReadLine( char *line ) // line is a string
  2. {
  3. while ( (*line = getchar() ) != '\n' )
  4. line++;
  5.  
  6. *line = 0;
  7. }


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.
Reputation Points: 108
Solved Threads: 14
Junior Poster in Training
GloriousEremite is offline Offline
65 posts
since Jul 2006
Aug 16th, 2006
0

Re: [noob] introduction+strange loops.

Quote originally posted by Lerner ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 16th, 2006
0

Re: [noob] introduction+strange loops.

Maybe I should post ALL of the code before you/we indict this guy. I swear this book was great unitl page 172. :lol:

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.


  1. #include <stdio.h>
  2. #include <ctype.h> //This is to bring in the declaration of isspace()
  3. #include <c.h> //This is to bring in the define of true
  4.  
  5.  
  6. #define kMaxLineLength 200
  7. #define kZeroByte 0
  8.  
  9. void ReadLine( char *line );
  10. int CountWords( char *line );
  11.  
  12.  
  13. int main (int argc, const char * argv[])
  14. {
  15. char line[ kMaxLineLength ];
  16. int numWords;
  17.  
  18. printf( "Type a line of text, please:\n" );
  19.  
  20. ReadLine( line );
  21.  
  22. numWords = CountWords( line );
  23.  
  24. printf( "\n---- This line has %d word", numWords );
  25.  
  26. if ( numWords != 1 )
  27. printf( "s" );
  28.  
  29. printf( " ----\n%s\n", line );
  30.  
  31. return 0;
  32. }
  33.  
  34.  
  35. void ReadLine( char *line )
  36. {
  37. //printf("moopants\n");
  38. //printf("%d",getchar() );
  39. while ( (*line = getchar()) != '\n' )
  40. {
  41. line++;
  42. }
  43.  
  44. *line = kZeroByte;
  45. }
  46.  
  47.  
  48. int CountWords( char *line )
  49. {
  50. int numWords, inWord;
  51.  
  52. numWords = 0;
  53. inWord = false;
  54.  
  55. while ( *line != kZeroByte )
  56. {
  57. if ( ! isspace( *line ) )
  58. {
  59. if ( ! inWord )
  60. {
  61. numWords++;
  62. inWord = true;
  63. }
  64. }
  65. else
  66. inWord = false;
  67.  
  68. line++;
  69. }
  70.  
  71. return numWords;
  72.  
  73. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
puuukeey is offline Offline
4 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: code for detecting usb devices
Next Thread in C Forum Timeline: About CView problem,thank:)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC