Click here to Skip to main content
15,041,525 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
first code:
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:
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"
}


What I have tried:

i was confuse here in first code<pre>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

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!!!!!
Posted
Updated 12-Jun-17 12:26pm

OK, head is declared as a node* which means that head is a variable which contains a pointer to a node object.

Think about cars for a moment. You have a space in the car park where you have parked your car. If you let me drive it, you don't tell me "it's the red Ford Fiesta, registration number ABC 123" because I'd have to search the whole car park looking for it - you tell me it's parked on level 3, row B, bay 17: 3B17 is enough for me to find the car.
"3B17" is the address of the bay containing the car - or a pointer to the car. If I move the car, I return a different address: "4A12" and you can find the car and go home.

Similarly, head doesn't contain the first node in your list - it contains a number which tells you where that node is: the address of the node. And just like I did, I found your car by going to that address.
To go to the node, you follow the pointer - this is called dereferencing the pointer - by prefixing it with a '*' or by using the -> operator.

To get the address of anything, you prefix it with '&': so &head gives you the address of head, or a pointer to a pointer to a node.
And in C, all values are passed to functions by value (not by reference) which means that inside the function you use a copy of what is passed - any changes you make do not affect the outside world. This is handy:
C++
void DoIt(int i)
   {
   i = 2 * i;
   }
Is fine when you do this:
C++
int i = 666;
DoIt(i);
printf(i);
And you get the value 666 printed. If it passed by reference, you would print 1332 - which would also work, until you did this:
C++
DoIt(666);
Passing by value means that the function can't affect the outside world, so there is no attempt to change fundamental constants!

So when you write:
C#
Push(&head, 1);
You pass through the address of head, or a pointer to head (or a pointer to a pointer to a node) which means that head itself can be changed by the function.

When you use your foo function, a copy of the address of the 'c' in "cat" is passed to the function (by value, remember), the function changes the copy by auto incrementing it, and the copy is discarded at the end of the function, leaving the outside world unchanged.
   
In example 1 you have the statement:
C++
struct node* head = ...

which declares head as a pointer to a node structure. That is to say that head is a pointer variable and its value will be the address of a node structure; either the value returned by malloc or BuildTwoThree. In the call to Push you have parameter 1 as &head which passes the address of that variable (& is the addressof operator in C/C++). Passing the address allows the called function to modify the variable's contents.

In example 2 you pass the pointer, not its address, to the function (actually you pass a temporary copy). So any changes to the pointer in the function do not affect the original variable.
   
Quote:
Difference between pointer value, pointer address, pointer variable

this is the difficult part of C, I think the best is to read the reference book and do exercises.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
   
ok still i was confused in some point,,,

first code:
	

    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:

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900