Author |
Message |
aade #1 / 10
|
 k&r2 question 4.3
hi all and thank you for reading this in this small program from 4.3 page 72 #include <stdio.h> #define MAXLINE 100 /* rudimentary calculator */ intt mani() { double sum ,atof( char []); char line [MAXLINE]; int getline(char line[], int max); sum 0; while (getline(line,MAXLINE) > 0) printf("\t%g\n",sum +=atof(line)); return 0; Quote: }
whislt i get most of this routine i can yet get my head around what the line does while (getline(line,MAXLINE) > 0) where does line come from ?? is it from the int getline(char line .... line?? could some kind sole explain this line to me i am learming alone at home with no other comp user to ask regards aade
|
Sun, 18 Jan 2004 00:09:46 GMT |
|
 |
brea #2 / 10
|
 k&r2 question 4.3
Quote: > hi all and thank you for reading this > in this small program from 4.3 page 72 > #include <stdio.h> > #define MAXLINE 100 > /* rudimentary calculator */ > intt mani()
int main [snip] Quote: > while (getline(line,MAXLINE) > 0) > where does line come from ?? > is it from the int getline(char line .... line?? > could some kind sole explain this line to me > i am learming alone at home with no other comp user to ask
getline is a function that the authors wrote to read a complete line from stdin. It is first defined in page 69 (with 1 page shift, i have the french version), then in page 163 (same shift), thanks to the use of fgets. They declare it at the beginning of the function where it is used (so in main), like they declare functions non returning int type (e.g. atof). -- ------------ Break mail : convert the 2 in to for the valid e-mail http://homeusers.brutele.be/walkingtoadventure/
|
Sun, 18 Jan 2004 00:40:22 GMT |
|
 |
Bart v Ingen Schena #3 / 10
|
 k&r2 question 4.3
Quote:
>hi all and thank you for reading this >in this small program from 4.3 page 72 >#include <stdio.h> >#define MAXLINE 100 >/* rudimentary calculator */ >intt mani()
This should be int main() Quote: >{ > double sum ,atof( char []); >char line [MAXLINE]; /* line 9 */ >int getline(char line[], int max); >sum 0; >while (getline(line,MAXLINE) > 0) > printf("\t%g\n",sum +=atof(line)); >return 0; >} >whislt i get most of this routine i can yet get my head around what the line >does >while (getline(line,MAXLINE) > 0) >where does line come from ??
line is a variable that can store a string. line is defined in line 9 of your code (marked with a comment). Quote: >is it from the int getline(char line .... line??
No, it from one line higher up. Quote: >could some kind sole explain this line to me >i am learming alone at home with no other comp user to ask
The line int getline(char line[], int max); is an indication to the compiler that somewhere a function 'getline' exists that returns an int and takes an pointer to char[1] and an int as parameters. Quote: Bart v Ingen Schenau [1] This is the one situation where the array notation (char line[]) actually indicates a pointer.
|
Sun, 18 Jan 2004 15:30:42 GMT |
|
 |
aade #4 / 10
|
 k&r2 question 4.3
thanks Bart v Ingen Schenau for taking the time to explain so where in the declaration of getline and line[] is declared but does line then mean its a pointer to line[] array regards aade ------
Quote:
> >hi all and thank you for reading this > >in this small program from 4.3 page 72 > >#include <stdio.h> > >#define MAXLINE 100 > >/* rudimentary calculator */ > >intt mani() > This should be > int main() > >{ > > double sum ,atof( char []); > >char line [MAXLINE]; /* line 9 */ > >int getline(char line[], int max); > >sum 0; > >while (getline(line,MAXLINE) > 0) > > printf("\t%g\n",sum +=atof(line)); > >return 0; > >} > >whislt i get most of this routine i can yet get my head around what the > line > >does > >while (getline(line,MAXLINE) > 0) > >where does line come from ?? > line is a variable that can store a string. line is defined in line 9 of > your code (marked with a comment). > >is it from the int getline(char line .... line?? > No, it from one line higher up. > >could some kind sole explain this line to me > >i am learming alone at home with no other comp user to ask > The line > int getline(char line[], int max); > is an indication to the compiler that somewhere a function 'getline' exists > that returns an int and takes an pointer to char[1] and an int as > parameters. > >regards > >aade > Bart v Ingen Schenau > [1] This is the one situation where the array notation (char line[]) > actually indicates a pointer.
|
Sun, 18 Jan 2004 17:17:04 GMT |
|
 |
Zoran Cutur #5 / 10
|
 k&r2 question 4.3
Quote: > hi all and thank you for reading this in this small program > from 4.3 page 72 > #include <stdio.h> > #define MAXLINE 100 > /* rudimentary calculator */ > intt mani()
The book simply says main() but as you seem to have learned already that main should return an int, you would be writing int main() and even better, as your program doesn't expect any arguments you would explicitly say so: int main(void) Quote: > { > double sum ,atof( char []); > char line [MAXLINE]; > int getline(char line[], int max);
Note, that although used in this occasion in K&R2, function declarations usually wouldn't be made inside other functions. Usually one declares them at the begining of a translation unit, so they can be used in all functions of the unit. Anyhow, since in this little program main is the only function ready to call atof and getline, it is valid to declare them inside main. Note, that line is defined to be an array of char that can hold MAXLINE-1 characters plus a succeeding \0-byte which indicates the end of a string. Now this array is meant to be a buffer for reading one line at a time from users input. Quote: > sum 0; > while (getline(line,MAXLINE) > 0) > printf("\t%g\n",sum +=atof(line)); > return 0; > } > whislt i get most of this routine i can yet get my head around > what the line does > while (getline(line,MAXLINE) > 0)
getline() is a function expecting two arguments. The first shall be an array of char. Now that is will be a topic to discuss again when you move ahead with your studies. Your program passes an array of char called line to getline. So it tells getline, please use this buffer to put data in it. The second argument, is an integer value that tells getline how big the buffer passed in the first argument ist. That is helpful, so getline can check not to write aside the array you want to use. So you can see usually when we pass a variable to a function, only its value is copied and te function will be working with that value, but when we pass an array to a function, it will be working with that array and changes made to the contents of the array inside the function, will appear in the array after the function finishes. Quote: > where does line come from ?? > is it from the int getline(char line .... line??
No it's from char line[MAXLINE]; HTH, HAND --
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Sun, 18 Jan 2004 17:28:24 GMT |
|
 |
Jim Sta #6 / 10
|
 k&r2 question 4.3
Quote: > thanks Bart v Ingen Schenau > for taking the time to explain so where in the declaration of getline and > line[] is declared > but does line then mean its a pointer to line[] array
Since the array name 'line' is used as an argument in the function 'getline', 'line' will be converted into a pointer to the array's first element and it is this pointer that will be passed to the function 'getline'. So, the function receives a pointer to char.
|
Sun, 18 Jan 2004 16:57:56 GMT |
|
 |
Jim Sta #7 / 10
|
 k&r2 question 4.3
Quote:
> > hi all and thank you for reading this in this small program > > from 4.3 page 72 > > #include <stdio.h> > > #define MAXLINE 100 > > /* rudimentary calculator */ > > intt mani() > The book simply says > main() > but as you seem to have learned already that main should return > an int, you would be writing > int main() > and even better, as your program doesn't expect any > arguments you would explicitly say so: > int main(void) > > { > > double sum ,atof( char []); > > char line [MAXLINE]; > > int getline(char line[], int max); > Note, that although used in this occasion in K&R2, function > declarations usually wouldn't be made inside other functions. > Usually one declares them at the begining of a translation unit, > so they can be used in all functions of the unit. Anyhow, > since in this little program main is the only function > ready to call atof and getline, it is valid to declare them > inside main. > Note, that line is defined to be an array of char that can > hold MAXLINE-1 characters plus a succeeding \0-byte > which indicates the end of a string. Now this array is meant to > be a buffer for reading one line at a time from users input. > > sum 0; > > while (getline(line,MAXLINE) > 0) > > printf("\t%g\n",sum +=atof(line)); > > return 0; > > } > > whislt i get most of this routine i can yet get my head around > > what the line does > > while (getline(line,MAXLINE) > 0) > getline() is a function expecting two arguments. The first > shall be an array of char. Now that is will be a topic to > discuss again when you move ahead with your studies. > Your program passes an array of char called line to getline. > So it tells getline, please use this buffer to put data in it.
No. When the arrayname line is used as an argument in getline, line will first be converted into a pointer to the array's first element and that pointer is then passed to the function getline. So, what is passed to getline is not an array of char but a pointer to char.
|
Sun, 18 Jan 2004 17:10:15 GMT |
|
 |
Zoran Cutur #8 / 10
|
 k&r2 question 4.3
... Quote: >> getline() is a function expecting two arguments. The first >> shall be an array of char. Now that is will be a topic to >> discuss again when you move ahead with your studies. Your >> program passes an array of char called line to getline. So it >> tells getline, please use this buffer to put data in it. > No. When the arrayname line is used as an argument in getline, > line will first be converted into a pointer to the array's > first element and that pointer is then passed to the function > getline. So, what is passed to getline is not an array of char > but a pointer to char.
Absolutly correct, that is what I meant with "Now that will be a topic to discuss again when you move ahead with your studies". I just didn't want to confuse the OP with these facts as for the moment it wouldn't be absolutly necessary to understand this. --
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Sun, 18 Jan 2004 18:43:07 GMT |
|
 |
Zoran Cutur #9 / 10
|
 k&r2 question 4.3
Quote: > hi all and thank you for reading this in this small program > from 4.3 page 72 > #include <stdio.h> > #define MAXLINE 100 > /* rudimentary calculator */ > intt mani()
The book simply says main() but as you seem to have learned already that main should return an int, you would be writing int main() and even better, as your program doesn't expect any arguments you would explicitly say so: int main(void) Quote: > { > double sum ,atof( char []); > char line [MAXLINE]; > int getline(char line[], int max);
Note, that although used in the occasion in K&R2 function declarations wouldn't be made inside other functions. Usually one declares them at the begining of a translation unit, so they can be used in all functions of the unit. Anyhow, since in this little program main is the only function ready to call atof and getline, it is valid to declare them inside main. Note, that line is defined to be an array of char that can hold MAXLINE-1 characters plus a succeeding \0-byte which indicates the end of a string. Now this array is meant to be a buffer for reading one line at a time from users input. Quote: > sum 0; > while (getline(line,MAXLINE) > 0) > printf("\t%g\n",sum +=atof(line)); > return 0; > } > whislt i get most of this routine i can yet get my head around > what the line does > while (getline(line,MAXLINE) > 0)
getline() is a function expecting two arguments. The first shall be an array of char. Now that is will be a topic to discuss again when you move ahead with your studies. Your program passes an array of char called line to getline. So it tells getline, please use this buffer to put data in it. The second argument, is an integer value that tells getline how big the buffer passed in the first argument ist. That is helpful, so getline can check not to write aside the array you want to use. So you can see usually when we pass a variable to a function, only its value is copied and te function will be working with that value, but when we pass an array to a function, it will be working with that array and changes made to the contents of the array inside the function, will appear in the array after the function finishes. Quote: > where does line come from ?? > is it from the int getline(char line .... line??
No it's from char line[MAXLINE]; HTH, HAND --
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Sun, 18 Jan 2004 14:30:16 GMT |
|
 |
fe #10 / 10
|
 k&r2 question 4.3
Quote:
> ... > >> getline() is a function expecting two arguments. The first > >> shall be an array of char. Now that is will be a topic to > >> discuss again when you move ahead with your studies. Your > >> program passes an array of char called line to getline. So it > >> tells getline, please use this buffer to put data in it. > > No. When the arrayname line is used as an argument in getline, > > line will first be converted into a pointer to the array's > > first element and that pointer is then passed to the function > > getline. So, what is passed to getline is not an array of char > > but a pointer to char. > Absolutly correct, that is what I meant with "Now that will > be a topic to discuss again when you move ahead with your > studies". I just didn't want to confuse the OP with these facts > as for the moment it wouldn't be absolutly necessary to > understand this.
It would be necessary to understand this as your statement "Your program passes an array of char called line to getline" will probably confuse the OP even more , especially when he encounters "int getline(char *line, int max)". Also, this will serve as a foundation for the OP to understand the subtle differences between pointers and arrays later.
|
Sun, 25 Jan 2004 14:18:21 GMT |
|
|
|