Re: C code... file reading(twice)... strange problem!

Rohit Kumar <rohitsz2-/[email protected]> Mon, 21 Mar 2005 08:57:37 -0800 (PST)
Newsgroups gmane.user-groups.linux.delhi.devel
Message-ID <[email protected]>
Greetings again...

> Hi,
>        In the code written by U, I found that in
> main U have declared
> a pointer val which holds address of variable i.
> Therefore even though
> the value of i changes in the loop the address of
> val never changes
> and *val stores the last value held by variable i.
> Though U have
> written the value of var in file temp.dat I believe
> the value of
> variable var will always  be same and thereby the
> value of *var also
> holding the last value of i i.e (10).

but if *val has the value 10, how is it able to write
the numbers from 0-9 into the file correctly and read
it correctly at the first time(in main)?

>         Moreover, when U call the function
> getRandomNumArray(),
> certain most compilers tend to release the variables
> stored in the
> called function, thereby the value stored in those
> addresses may be
> junk values, but this not always the case. It might
> just help if U
> declared variable i as a global variable.
i have been trying my code on Turbo C and gcc... so i
do not get junk values as i check the values that are
read from the file in the function getRandomArray()
with the vales returned to main (base pointer). they
appear to be identical. so junk values/loss of value
due to release of variable is not there in this code.

> But if U want to save all the values of i I
> recommend U to save the
> intermediate values of i and not its address.

i am doing 
            val=&i;
because the fread()/fwrite() take pointer as first
argument, so need to put value of i in a pointer and
the send it to fread()/fwrite()

how do i do it otherwise??

> Sorry for the delay, I was really busy and could'nt
> check my mail.
no problem here, would have waited anyways, thanks a
lot for peeking into the code! :-)

PS:today i tried little modification with the code
which incidated my not so good understanding of
pointers..(hit & trial)
in the following three commands
 fwrite()  in main()
 fread()    in main()
 fread()    in getRandomArray()

rather than passing '&val' as first argument, i send
'val' and he code worked successfully on TC and NOT on
GCC where it gave segmentation fault error!

so maybe this might help in resolving my problem, a
little!

thanks again,
-rohit.

NAMASTE, i honour the spirit in you which is also in me.
```````````````````````````````````````````````````````
http://www.geocities.com/rohitsz2

|Anytime you feel i need to understand| 
|a concept before i am eligible to get|
|an answer i would be happy to know.  |
^*************************************^


		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - Easier than ever with enhanced search. Learn more. 
http://info.mail.yahoo.com/mail_250