Thread: dereference a pointer in a function

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    6

    dereference a pointer in a function

    This is probably a very simple question but i can't solve the problem :/

    got:

    Code:
    void dereference(int* a, int* b)
    {
        a=b;
    }
    
    int main(int argc, char **argv)
    {
        int c = 4;
        int c1 = 3;
        int* d = &c;
        int* f = &c1;
        dereference(f,d);
        printf("b: %d\n",(*f)); // b = 3
        
        f=d;
        printf("b: %d\n",(*f)); // b=4
        
        return 0;
    }
    Why isn't f and d the same after calling "dereference(f,d);"

  2. #2
    Registered User
    Join Date
    Feb 2015
    Posts
    6
    Hi! Its a good question. I am new to c and I think in your main function where f = d you are shifting value of d to f but in reality its not happening. Look *f points to value of c1 so it should be something like this:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void dereference(int* a, int* b);
    
    int main(int argc, char **argv)
    {
      int c = 4;
      int c1 = 3;
      int* d = &c;
      int* f = &c1;
      dereference(f,d);
      printf("b: %d\n",(*f)); // b = 3                                              
    
      c = c1;
      // f=d;                                                                       
    
      printf("b: %d\n",(*f)); // b=4                                                
    
      return 0;
    }
    
    void dereference(int* a, int* b)
    {
      a=b;
    }
    I am sorry since I am new to the programming but I think it should happen. Please share if you think I am wrong.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,595
    Probably because you're changing where the pointer points, not the value held in the variable. And since the pointers are passed by value any changes made to the pointers are lost when the function terminates.

    You may want to try adding some print statements to see what is actually happening:

    Code:
    void dereference(int* a, int* b)
    {
       printf("%p %p\n", (void*)a , (void*)b);
       a=b;
       printf("%p %p\n", (void*)a , (void*)b);
    }

    Jim

  4. #4
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    Quote Originally Posted by royalts View Post
    This is probably a very simple question but i can't solve the problem :/

    got:

    Code:
    void dereference(int* a, int* b)
    {
        a=b;
    }
    
    int main(int argc, char **argv)
    {
        int c = 4;
        int c1 = 3;
        int* d = &c;
        int* f = &c1;
        dereference(f,d);
        printf("b: %d\n",(*f)); // b = 3
        
        f=d;
        printf("b: %d\n",(*f)); // b=4
        
        return 0;
    }
    Why isn't f and d the same after calling "dereference(f,d);"
    Remember that a and b are local variables for your dereference function, which have no meaning outside the function.
    Within the function you assign the value of the pointer b (not the value b is pointing to) to the pointer a. As soon as you leave the function the assignment is forgotten.
    If you want to assign the value b is pointing to, you have to dereference the pointers. which means to use *a = *b (assign the value b is pointing to, to where a is pointing to).

    Cheers

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    6
    Thx for your fast reply
    one thing is still unclear, why is the dereferencing after exit the function "dereference" constistent but not the assigning of the value? Because this looks to me the fastest way to assign the pointer a value... especially when it is a huge struct.

    ps: and i need a fast solution

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,312
    Quote Originally Posted by royalts
    one thing is still unclear, why is the dereferencing after exit the function "dereference" constistent but not the assigning of the value? Because this looks to me the fastest way to assign the pointer a value... especially when it is a huge struct.
    When you assign a pointer x to another pointer y, y points to where x points. Therefore, if you dereference x, you get the same value as if you dereferenced y, so it is no surprise that it works in the main function. The problem in your example is that you were doing the pointer assignment within another function, in which case you are operating on copies of the original pointers from the main function.

    Yes, if you have something that is expensive to copy, then passing a pointer to it makes sense when you don't need a copy of it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2015
    Posts
    6
    I thought if the arguments of a function are pointers e.g. (int* a, int* b) I work with the adresses a and b points to and so no copy's of the values would be made. So that I can change consistently the values?! Now I'm really confused :/

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,312
    Quote Originally Posted by royalts
    I thought if the arguments of a function are pointers e.g. (int* a, int* b) I work with the adresses a and b points to and so no copy's of the values would be made.
    Yes. However, the pointers themselves are copied. Compile and run:
    Code:
    #include <stdio.h>
    
    void foo(int *p)
    {
        printf("*p: %d\np: %p\n&p: %p\n", *p, (void*)p, (void*)&p);
    }
    
    int main(void)
    {
        int x = 123;
        int *px = &x;
        printf("*px: %d\npx: %p\n&px: %p\n", *px, (void*)px, (void*)&px);
        foo(px);
        return 0;
    }
    Notice that *px and *p are the same: indeed, in the context of the function call foo(px), both pointers point to x.

    Notice that the values of px and p are the same: indeed, in the context of the function call foo(px), p is a copy of px.

    Notice that addresses of px and p (i.e., &px and &p respectively) are different: indeed, px is a local variable in main; p is a local variable in foo, hence they are different objects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2015
    Posts
    6
    O.k. that make sense...
    Great explained!!!! Thanks for that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer dereference problem in sortFunction
    By csharp100 in forum C Programming
    Replies: 15
    Last Post: 04-19-2012, 06:35 PM
  2. null pointer dereference
    By qwertylurker in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 12:06 AM
  3. Suspicious dereference of pointer
    By subhashish1213 in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2009, 08:19 AM
  4. Pointer dereference
    By taurus in forum C Programming
    Replies: 1
    Last Post: 11-09-2008, 07:41 AM
  5. Dereference pointer to void pointer to member
    By phil in forum C Programming
    Replies: 5
    Last Post: 04-20-2005, 11:54 AM