Mail Archives: djgpp/1999/01/18/14:00:32
My compliments on actually trying to walk your code. Howevee there are
a number of warts (such as writing void main() ) that I guess others
will point out to you. However you have walked into a well known trap
which I highlight below:
In article <clcm-19990117-0010 AT plethora DOT net>, Mohanaraj Gopala Krishnan
<saradiya AT bigfoot DOT com> writes
>void open_file(int * pages_done, int array_size)
Would be a good idea to have this function return a suaccess/failure
indicator
>{
> int i=0;
>
> if((fptr=fopen("chapter.dat", "r")) !=NULL) {
>
>fseek(fptr,0L,SEEK_SET);
>
>for(i=0;i<array_size;i++) {
OK you count through from 0 to array_size
>
>fscanf(fptr,"%d",*(pages_done++));
but here we have a very nasty trap
pages_done was a pointer to an int (I guess it is to be an array)
that means *(pages_done++) evaluates to the CONTENTS of the current
address in pages_done before incrementing the address. But if you check
fscanf you will find that it needs an address of an int.
you need to write:
fscanf(fptr,"%d", pages_done+i);
You could use the increment operator but I find the base+offset better
in this type of situation, though that is a matter of personal style.
>
>}
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--
comp.lang.c.moderated - clcm AT plethora DOT net
- Raw text -