# Thread: dereference a pointer in a function

1. ## 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. 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. 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. Originally Posted by royalts 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. 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. 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. 7. 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. 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. 9. O.k. that make sense...
Great explained!!!! Thanks for that  Popular pages Recent additions 