473,382 Members | 1,464 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,382 software developers and data experts.

function getinteger from kernighan ritcie book

82
The following loop fills an array with integers by calls to getint
[PHP]int main(void)
{
int n, array[SIZE], getint(int *);

for(n=0; n<SIZE && getint(&array[n]) !=EOF; n++)
;

return 0;
}


/*int getch(void);
void ungetch(int);*/ ignore it

int getint(int *pn)
{
/* int c,sign;

while(isspace(c=getch()))
;
if(!isdigit(c) && c!=EOF && c!='+' && c!='-'){
ungetch(c);
return 0;
}

sign = (c == '-') ? -1 : 1;
if(c == '+' || c == '-')
c=getch();*/ ignore it
for(*pn = 0; isdigit(c); c = getch())
*pn = 10* *pn + (c - '0');
*pn *= sign;
if (c!=EOF)
ungetch(c);

return c;
}
[/PHP]

In line 5 we have a loop that fills the array with integers.Then in line 29 we have another loop that returns the integers in the array.But i don't understand line 30, why we use 10* *pn?? In position of getch we can put getchar.Thanks
Aug 24 '07 #1
6 1583
JosAH
11,448 Expert 8TB
In line 5 we have a loop that fills the array with integers.Then in line 29 we have another loop that returns the integers in the array.But i don't understand line 30, why we use 10* *pn?? In position of getch we can put getchar.Thanks
That function takes a pointer to an int where the result is supposed to be stored.
Suppose the characters '1', '2' and '3' are read from the input stream. First the
value '1'-'0' == 1 is stored in that location, the next loop pass multiplies that
value by 10 and add the new value '2'-'0' == 2; so the result is 10+2 == 12.
The third loop pass does the same and the result is 12*10+3 == 123, exactly
what you wanted.

kind regards,

Jos
Aug 24 '07 #2
kalar
82
The third loop pass does the same and the result is 12*10+3 == 123, exactly
what you wanted.

kind regards,

Jos
yes but we want separately the '3' on array[2] and not 123.Or you mean that we type '123' as one number? I don't understand.Sorry
Aug 24 '07 #3
JosAH
11,448 Expert 8TB
yes but we want separately the '3' on array[2] and not 123.Or you mean that we type '123' as one number? I don't understand.Sorry
Well, you've copied the wrong function for your purposes then. You could change the body of the loop in that function to:

Expand|Select|Wrap|Line Numbers
  1. *pn++= c-'0';
  2.  
... but you have to take some precautions w.r.t. buffer (array) overflow then.

kind regards,

Jos
Aug 24 '07 #4
kalar
82
edit:
sorry my mistake now i found it
Aug 24 '07 #5
JosAH
11,448 Expert 8TB
I don't think so
A) because the function is from kernighan and ritcie book and i don't find any mistake in the errata list from the book
B)and mainly
then why works both af them correct(also *pn++= c-'0'; worked with the same result)??
A) I immediately agree that there are no errors in that little function but it doesn't
do what you had in mind (see your previous reply). You have to make up your
mind: do you want:

A1) the three characters 123 to be interpreted as the single integer value 123 or
A2) do you want three separate integer values 1, 2 and 3?

B) I don't believe that K&Rs original version and my quick hack yield the same result.

kind regards,

Jos
Aug 24 '07 #6
kalar
82
A) I immediately agree that there are no errors in that little function but it doesn't
do what you had in mind (see your previous reply). You have to make up your
mind: do you want:

A1) the three characters 123 to be interpreted as the single integer value 123 or
A2) do you want three separate integer values 1, 2 and 3?

B) I don't believe that K&Rs original version and my quick hack yield the same result.

kind regards,

Jos
As i said my mistake.I try the same numbers in the two functions
1 enter
2 enter
3 enter
.
.
.
So i had the same result.Now i understand it(how work both functions).Thanks again
Aug 24 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Pushkar Pradhan | last post by:
I want a function to execute another function, which I pass to it, sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style. double exec_basecase(void (*func)(double *a, double...
15
by: interpim | last post by:
Im trying to teach myself programming, and I am doing exercises out of the book C by Dissection by Ira Pohl. I am stuck on one of them and can't figure out how to fix it. The exercise is for you...
5
by: Johnathan Doe | last post by:
Why is is necessary to write a function that allocates memory for, or changes, a pointer to a char array using char** instead of char*? E.g. void modify_string( char** string ) { if( string...
90
by: Jhon smith | last post by:
Hi all,Just wondering are there any problems with learning c from older books,as I have picked up some from 1988,1994,1997,1998. By using books of this age(Im on a tight budget)am I going to...
9
by: niclane | last post by:
Hi, I was reading section 5.5 of Ritchie and Kernighan and saw the following: " ..... char amessage = "now is the time"; char *pmessage = "now is the time";
19
by: sabarish | last post by:
Hi friend, what is the use of function pointer in c language and where it is useful? tell with simple example...? plz help me.
6
by: dndfan | last post by:
Hello, In the short time I have spent reading this newsgroup, I have seen this sort of declaration a few times: > int > func (string, number, structure) > char* string > int number >...
33
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
6
by: varojee | last post by:
hi! can we use any function to display our input without using a " printf " function? does c support such this possibilities? ...
1
by: kaili | last post by:
hi i'mchinese. i heard that "C Programming Language" by Brian W. Kernighan, Dennis is the best book for a new learner but it's unavailable in xi'an a Northwestern city of China, i mean ,there's...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.