difference between pointer variable, pointer value, pointer address, variable address

sorry i post code in c!!!

first code:
1
2
3
4
5
6
7
8
9
10
11
12
void Push(struct node** headRef, int data) {
struct node* newNode = malloc(sizeof(struct node));
newNode->data = data;
newNode->next = *headRef; // The '*' to dereferences back to the real head
*headRef = newNode; // ditto
}
void PushTest() {
struct node* head = BuildTwoThree();// suppose this returns the list {2, 3}
Push(&head, 1); // note the &
Push(&head, 13);
// head is now the list {13, 1, 2, 3}


second code:
1
2
3
4
5
6
7
8
9
10
11
12

void foo(char *ptr) {
    ptr++; // Move the pointer (but it's pass by value)
}
void bar() {
    char *str = "cat";
    char *ptr = str;
    foo(ptr);
    printf("%s\n", ptr); // Prints "cat"
}




i was confuse here in first code inside function PushTest, when it calls Push(&head, 1)

it calls Push and give head address, head is a pointer to a struct node that inside it, it also point to another node.
&head here means the address of head/address of what is store inside head?
what actually it pass to Push function??
and what actually Push receive??

in second code, there is foo(ptr), calls foo and pass ptr, ptr is a pointer to a string and it point "c" in "cat", but actually what is being passed to function foo?
is it the address of ptr/address of "c" in "cat"??
i was confused with pointer value, pointer address, and pointer variable address itself!!!!!
thankyou!!
Last edited on
void Push(struct node** headRef, int data)

push takes a ** for some reason. So if what you have in main is
node* head;
you have to call push with
push(&head, ...) //now its a **, you took the address of head so it would meet the criteria of the push function parameter.

ideally, from looking at the code, this seems pointless and you should instead have
void Push( node * headref, ... // no double pointer needed. I could be wrong, maybe there is a reason for it, but I can't see what it is.

ptr is a pointer to a single character. somewhere in memory, there are 4 bytes in a row, c,a,t,0. Ptr points to the 'c' in cat. Foo(ptr) moves ptr once, so it now points to the 'a' instead, WHILE INSIDE FOO. ptr was not passed by reference so it comes back out unmodified, and still points to the c in cat. You print it, and the print function knows to print all the letters from the start until it hits the zero end of string marker, so it prints cat.

You have to be careful with passing pointers to functions. The pointer won't be modified if not passed by reference, but the data pointed to CAN BE. If you had, inside foo, said p[1] = 0, the program should now print out "c" and stop. Try it! Does this make sense? (It may also just crash, since its actually hacking on a string literal, if it crashes change the program to be

char *str = new char[10];
strcpy(str,"cat");

Last edited on
@jonnin thankyou for answering my question!
struct node* head; in main,
here head is a pointer to struct node
head is pointing to struct node { data, struct node*next(in order to link to another node)}
head is pointer variable where address of struct is store in it
head variable is somewhere in memory address suppose 69, but it contains memory address to store data struct suppose 1234
push (&head) here, pass 1234 or 69?

the data pointed can be changed because, func(ptr) pass a memory address which is "cat" is stored inside of it or &ptr[0] right?????
thats why foo(char*ptr) will define it as a copy, but still point to same &ptr[0] , thats why it changed??
everything has a memory location :)

ok, lets say

head is at memory location 100.
head stores 200, which is another memory location (where the data we point to resides).
200 stores "data"

&head is 100.
head is 200.
head[0] or *head are "data".

push(&head, ...) //&head is 100, its a pointer to head, with no name, just the value.

this is the same thing as
node ** x;
x = &head;
push(x,...);

and x[0][0] is "data".

so to answer in your terms, its neither 1234 nor 69, its another value that represents the address of the variable head which in turn represents the address of a (unnamed) node which in turn holds 69.

--------------------

you need to understand how the call stack works to understand this.
when a function is called, it creates a COPY of the parameters that are not references. Those copies are not copied back over the originals when the function exits, so any changes to it are lost. In the case of a pointer though ... the pointer value, the memory location number value, that is what was copied. Not the data at that location. If you change the pointer, it reverts, it was a copy. If you change the pointed-to data, it is changed for real, it was not a copy of the original. You literally went to a memory location and changed it, and its changed now. So if the pointer is seeing address 1234, and you go to location 1234 and modify it, that is changed. If you change 1234 to 1235, when the function exits, its back to 1234, as the 1235 was made to the temporary copy.




@jonnin thanks for the explanation!!!
So i can think that &head is a layer of pointer?
Its like i made a pointer to head and pass it to function?
Although head itself is also a pointer?
But actually by passing & in calling function has the same as passing the memory address that head is point to?
Pointer seeing address 1234, to go to that location and modify it, we have to pass a pointer to that address though we pass a copy of the ponter to another function, but it still refer to same memory address 1234 thats why we can modify value inside it because we pass it by reference?
And we can modify value inside it by deferencing the pointer?
Topic archived. No new replies allowed.