Re: A NULL pointer changed after returned?
From: fix (fix_at_here.com)
Date: 02/29/04
- Next message: fix: "Re: A NULL pointer changed after returned?"
- Previous message: Darklight: "Re: is this correct"
- In reply to: Ben Pfaff: "Re: A NULL pointer changed after returned?"
- Next in thread: Ben Pfaff: "Re: A NULL pointer changed after returned?"
- Reply: Ben Pfaff: "Re: A NULL pointer changed after returned?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 29 Feb 2004 11:13:24 -0600
Ben Pfaff wrote:
> fix <fix@here.com> writes:
>
>
>>void normalize(Fraction *fr){
>> int factor;
>> if (fr->denominator == 0) // Invalid function
>> {
>> fr = NULL; // set it to NULL and return
>>
>> printf("Set fr = NULL\n");
>> if (fr!=NULL)
>> printf("Not null");
>> else
>> printf("null");
>> return;
>
>
> This is in the FAQ.
>
> 4.8: I have a function which accepts, and is supposed to initialize,
> a pointer:
>
> void f(int *ip)
> {
> static int dummy = 5;
> ip = &dummy;
> }
>
> But when I call it like this:
>
> int *ip;
> f(ip);
>
> the pointer in the caller remains unchanged.
>
> A: Are you sure the function initialized what you thought it did?
> Remember that arguments in C are passed by value. The called
> function altered only the passed copy of the pointer. You'll
> either want to pass the address of the pointer (the function
> will end up accepting a pointer-to-a-pointer), or have the
> function return the pointer.
>
> See also questions 4.9 and 4.11.
Ah...... I got you, so that means if I change the content in the struct,
i.e. changing the signs, denom and numerators, the pointer hasn't
changed, but if I point it to NULL, I am actually changing the pointer
so it reverts to the original value outside the function body.
If I am to return a pointer, I am curious that if I return a pointer to
a variable created inside the function, will the variable be garbage
collected but the pointer still pointing to that? I was learning Java
last semester and I have no trouble with pointers.......
- Next message: fix: "Re: A NULL pointer changed after returned?"
- Previous message: Darklight: "Re: is this correct"
- In reply to: Ben Pfaff: "Re: A NULL pointer changed after returned?"
- Next in thread: Ben Pfaff: "Re: A NULL pointer changed after returned?"
- Reply: Ben Pfaff: "Re: A NULL pointer changed after returned?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|