Re: A NULL pointer changed after returned?

From: fix (fix_at_here.com)
Date: 02/29/04


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.......



Relevant Pages

  • Re: Need some help with dereferencing structures and such
    ... > struct products { ... the pointer to the data is a void pointer: ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Function Question
    ... > frame because variables are always global inside struct. ... > inside pointer to function list. ... Consider using a Function Object or Functor. ... C++ Faq: http://www.parashift.com/c++-faq-lite C Faq: http://www.eskimo.com/~scs/c-faq/top.html alt.comp.lang.learn.c-c++ faq: ...
    (comp.lang.cpp)
  • Re: Serial related oops
    ... We inserted 37 printk's in the function body, ... there's still little clue about why this is causing a NULL pointer ... The reported dump shows that the kernel tried to access virtual address 0, ...
    (Linux-Kernel)
  • [PATCH 07/15] ia64: add dma_capable() to replace is_buffer_dma_capable()
    ... if a memory area is dma-capable or not. ... is_buffer_dma_capableis that it doesn't take a pointer to struct ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)
  • [PATCH 08/15] powerpc: add dma_capable() to replace is_buffer_dma_capable()
    ... if a memory area is dma-capable or not. ... is_buffer_dma_capableis that it doesn't take a pointer to struct ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)