I have something that I don't know how to use the Pointer, this is my code:

struct node{
   int data;
   struct node* next;
}

struct node* BuildWithLocalRef() {
//give a list with: 1 2 3 4 5
//and your work is import this list in to Linked List with that order
//so linked List after finish will have:
//1-2-3-4-5
struct node* BuilList(){ 
    struct node* head = NULL;
    struct node** lastPtrRef= &head; 
    int i;
    for (i=1; i<6; i++) {
    Push(lastPtrRef, i); //give new element with value i into Linked List and before lastPtrRef node. 
    lastPtrRef= &((*lastPtrRef)->next); 
}
// head == {1, 2, 3, 4, 5};
return(head);
}

In that above code, there are 2 points that I don't know:
1)at line,

struct node** lastPtrRef=&Head

: I don't know what this line mean. I often use:

struct node** lastPtrRef=*Head

to point what *Head point.
2) at line,

lastPtrRef= &((*lastPtrRef)->next);

maybe I met same problem above, that I don't know use "&(*....) mean.

with my knowledge, "&" operator to take some one address. but I don't know the different purpose of two type of declare: with above example is:

struct node** lastPtrRef=*Head;//type 1
struct node** lasPtrRef=&Head; //type 2

so, who know this problem, please answer for me, please. many things in pointer that I don't know although I read basic document about Pointer :(

thanks for tons :)

Edited 5 Years Ago by hqt: n/a

but I don't know the different purpose of two type of declare: with above example is:

struct node** lastPtrRef=*Head;//type 1
struct node** lasPtrRef=&Head; //type 2

This is what I think your question boils down to, and the answer is that the first declaration is wrong while the second is right. The first shouldn't even compile because the type of *Head is struct node , not struct node** because Head is already a pointer to struct node .

Pointers are often overcomplicated by beginners. They're really very simple: a pointer holds an address. The type of a pointer is the type of the object stored at the address. The type of a pointer can also be a pointer (ie. pointers can be recursively defined with the base case being a non-pointer object).

Consider the following program:

#include <stdio.h>

int main(void)
{
    int x = 22;

    int *p = &x;
    int **pp = &p;
    int ***ppp = &pp;

    printf(" x     == %d\n", x);
    printf("&x     == %p\n\n", (void*)&x);
    
    printf("*p     == %d\n", *p);
    printf(" p     == %p\n", (void*)p);
    printf("&p     == %p\n\n", (void*)&p);

    printf("**pp   == %d\n", **pp);
    printf(" *pp   == %p\n", (void*)*pp);
    printf("  pp   == %p\n", (void*)pp);
    printf(" &pp   == %p\n\n", (void*)&pp);
    
    printf("***ppp == %d\n", ***ppp);
    printf(" **ppp == %p\n", (void*)**ppp);
    printf("  *ppp == %p\n", (void*)*ppp);
    printf("   ppp == %p\n", (void*)ppp);
    printf("  &ppp == %p\n", (void*)&ppp);
    
    return 0;
}

If you understand why the output is the way it is, you understand pointers. Things are complicated slightly when you throw array and member notation into the mix, such as with this statement:

lastPtrRef = &((*lastPtrRef)->next);

lastPtrRef is a pointer to a pointer to struct node , so to get to the next member, lastPtrRef first needs to be dereferenced. next is a pointer to struct node , so to be a valid assignment back to lastPtrRef , it needs an extra level of indirection. That's why the address is used. You can break the statement down to simplify it:

struct node *tmp = *lastPtrRef;

lastPtrRef = &tmp->next;

However, note that the following doesn't work because next is a separate object from tmp->next and has a different address. Thus lastPtrRef would point to the wrong object:

struct node *tmp = *lastPtrRef;
struct node *next = tmp->next;

lastPtrRef = &next;

I point that out because it's a common conceptual error to forget that pointers themselves are objects, and each has its own address.

thanks for your very kindly explanation :)after practice to much with "&" "*" "**", now, a pointer and pointer to pointer are more clearer to me.
but I have some problems about pointer that I cannot answered by myself, yet. First, I have one code:

int main(){
    struct node* head=BuilList(); //buil a linked list: example that 1-2-3-NULL
    printf("%d",*head);
}
//---> the screen will print 1 ( the first element of Linked List)

1)(*Pointer) will point to something that it points. So, at above example, *head point to a node that has type <struct node> consist two part: data and *next. why that C will print 1---> just "data" part ?

2) my second question is: because *head will print 1.So I have two line of code:

int main(){
     struct node* first,second;
     first=1;   //                                                (*) 
     printf("%d\n",first);// no error and will print 1
     second=*head;// ERROR (**)
}

I cannot explain why (**) is error. (don't laugh at me if the above code is meaningless)

thanks a ton :)

Edited 5 Years Ago by hqt: n/a

Sorry for my typo, I want to change but maybe too late, "Edit" button cannot active now.
In above text, I have typing wrong, second is a pointer (but I doesn't type "*" yet)
so:

struct code *first, *second;

sorry for all :(

Edited 5 Years Ago by hqt: n/a

1) Printing a structure object as an integer isn't meaningful. That it works at all is largely due to lack of type checking in functions with a variable argument list (eg. printf()) and the ordering of members in your structure.

2) first = 1 isn't a meaningful initialization for a pointer. Remember that the value of a pointer is an address, is 1 a valid address for a node object?

in my second question, that's just my test :), but I cannot explain why it's a compile error. (at line:second=*head). (I know that work is no meaning, and noone do that but know this problem, maybe give me an opportunity to avoid another problem might I will meet in some future.)
Now, with your answer about first question, I think wrong point is: *head=1 just maybe because printf function, and second=*head is wrong because we cannot do that :)

Edited 5 Years Ago by hqt: n/a

You're dereferencing a bogus pointer. You can't assign any random number to a pointer and expect it to be a valid address.

This article has been dead for over six months. Start a new discussion instead.