Help with pointer issue

This is a discussion on Help with pointer issue within the C Programming forums, part of the General Programming Boards category; Firstly, I'm new to C, so please bear with me. I'm trying to increment a pointer, the following code works ...

  1. #1
    ph5
    ph5 is offline
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    Help with pointer issue

    Firstly, I'm new to C, so please bear with me.

    I'm trying to increment a pointer, the following code works correctly with the
    following output:

    before number = [0], address = [0xbfec32b0}
    after number = [1], address = [0xbfec32b4}

    Code:
    #include <stdio.h>
    
    int main(void) {
    
        int number[2] = { 0,1 };
        int *p_number = NULL;
    
        p_number = &number[0];
    
        printf("before number = [%d], address = [%p}\n",*p_number,p_number);
    
        p_number++;
    
        printf("after number = [%d], address = [%p}\n",*p_number,p_number);
    
        return 0;
    
    }
    However, when I try to perform the same task using a function, I get the following output:

    before number = [0], address = [0xbff372a0}
    after number = [0], address = [0xbff372a0}

    Code:
    #include <stdio.h>
    
    void increment(int *p_address);
    
    int main(void) {
    
        int number[2] = { 0,1 };
        int *p_number = NULL;
    
        p_number = &number[0];
    
        printf("before number = [%d], address = [%p}\n",*p_number,p_number);
    
        increment(p_number);
    
        printf("after number = [%d], address = [%p}\n",*p_number,p_number);
    
        return 0;
    
    }
    
    
    void increment(int *p_address) {
    
         p_address++;
    
    }
    Any advice with this would be most welcome, I'm positive that I'm doing something wrong.

    Thanks
    ~P

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Pointers are values. The value is a memory address. If you need a value change done inside a function, to carry over outside of the function, you need a pointer to the object containing that value. Not the value itself. Consider:
    Code:
    void foo( int x )
    {
        x++;
    }
    If we pass a value here, and we increment it, that is lost when the function returns. The reason is, we're not actually passing a variable, we're passing a value. How? Well, because this is legal:
    Code:
    foo( 5 );
    That happens with everything ever passed to functions. They're all just values. If you need to change a variable's held value, then you need a pointer to that variable, not its value:
    Code:
    void foo( int *x )
    {
        (*x)++;
    }
    The same goes for your pointer. You will need a pointer to that pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Look what exactly happens in the function in C. Parameters are passed by value, so a copy is made
    Code:
    void increment(int *p_address) {
         p_address = PARAMETER_PASSED;
         p_address++;
    
    }
    so if you do
    Code:
    increment(p_number);
    you have
    Code:
    {
      p_address = p_number;
      p_address++;
    }
    So p_number doesn't increase, only the local variable p_address which is destroyed after the function ends. You have two options. First is to return p_address so you can assign the value, like
    Code:
    void increment(int *p_address) {
         p_address++;
         return p_address;
    }
    and use
    Code:
    p_number = increment(p_number);
    or pass by reference, like
    Code:
    void increment(int **p_address) {
         (*p_address)++;
    }
    and don't change anything else.
    Dunno if this makes sense to you

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Thumbs up

    Quote Originally Posted by quzah View Post
    The same goes for your pointer. You will need a pointer to that pointer.
    Eg.
    Code:
    increment(&p_number);
    void increment(int **p_address) {
         (*p_address)++;
    }
    So you pass the address of the pointer (which is a pointer to the pointer), then you increment the value at that address by dereferencing it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    ph5
    ph5 is offline
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Sorted. Many Thanks to all for the advice, I've also seen a pratical example of a pointer to a pointer. Excellent.

    Thanks
    ~P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 03:29 PM
  3. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2007, 12:26 AM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21