1

I am learning data structure, and here is a thing that I am unable to understand...

int end(struct node** p, int data){
    /*
    This is another layer of indirection. 
    Why is the second construct necessary? 
    Well, if I want to modify something allocated outside of my function scope,
    I need a pointer to its memory location. 
    */
    
    struct node* new = (struct node*)malloc(sizeof(struct node));
    struct node* last = *p;
    new->data = data;
    new->next = NULL;
    while(last->next !=NULL){
        last = last ->next ;
    }
    last->next = new;
}
  1. why we are using struct node **p?
  2. can we use struct node *p in place of struct node **p? the comment which I wrote here is the answer I found here, but still, I am unclear about this here is the full code...

please help me thank you

bereal
  • 32,519
  • 6
  • 58
  • 104
  • Does this answer your question? [Why use double indirection? or Why use pointers to pointers?](https://stackoverflow.com/questions/5580761/why-use-double-indirection-or-why-use-pointers-to-pointers) – Paul Rooney Sep 30 '21 at 06:57
  • 1
    Are you sure you copied this code correctly? As it is, it seems wrong. And.. as it is there is no need for a double-pointer. I think something like: `if (*p == NULL) { ...}` is missing. – Support Ukraine Sep 30 '21 at 06:57
  • it's not a complete code it is just function where I was confused – abhinav srivastava Oct 01 '21 at 05:54
  • Just in case you are not aware of it: The best way to add info and to clarify things, is to [edit] your question. E.g. for doing things as discussed in comment on my answer. – Yunnosch Oct 01 '21 at 05:55

2 Answers2

0

The shown function (according to its name) should create a new node and apend it at the end of the list represented by the pointer to a pointer to a node of that list. (I doubt however, that it actually does, agreeing with comments...)

Since the list might be empty and that pointer to node hence not be pointing to an existing node, it is ncessary to be able to potentially change the pointer to the first elemet of that list away from NULL to then point to the newly created node.

That is only possible if the parameter is not only a copy of the pointer to the first node but instead is a pointer to the pointer to the first node. Because in the second case you can dereference the pointer to pointer and actually modify the pointer to node.

Otherwise the list (if NULL) would always still point to NULL after the function call.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • While this is correct in the "normal case", it doesn't really apply here because the posted code doesn't check for `*p == NULL` – Support Ukraine Sep 30 '21 at 07:02
  • @4386427 Yes, the shown code is not doing as it should. That is why I refer to the intended functionality (as implied by the name and the fact of using a `**` parameter) and state that I dount the shown code. The question as asked "what does this struct node **p is doing?" and 1. and 2. I answer. – Yunnosch Sep 30 '21 at 07:04
  • It's not full code, should I share the complete code – abhinav srivastava Oct 01 '21 at 05:49
  • StackOverflow never wants "full code" (at least not in all those cases where it is a lot). What is appreciated here are questions which provide a [mre]. In thise case here (if I read your question correctly) it would be helpful clarification to point out that you are aware that the function does not (yet) act in all cases as expected (my guess/assumption), but that you are not asking about how to fix that and instead just want explanation on the meaning and working of `**pointer` constructs. If I did misread anything then clarfiy and point out my misunderstandings. – Yunnosch Oct 01 '21 at 05:54
  • My pleasure. Have fun on StackOverflow. At your convenience, please take the [tour] and you might find the info at [ask] helpful. – Yunnosch Oct 01 '21 at 05:56
0

Short answer: There is no need for a double-pointer in the posted code.

The normal reason for passing a double-pointer is that you want to be able to change the value of a variable in the callers scope.

Example:

struct node* head = NULL;
end(&head, 42);
// Here the value of head is not NULL any more
// It's value was change by the function end
// Now it points to the first (and only) element of the list

and your function should include a line like:

if (*p == NULL) {*p = new; return 0;}

However, your code doesn't !! Maybe that's really a bug in your code?

Since your code doesn't update *p there is no reason for passing a double-pointer.

BTW: Your function says it will return int but the code has no return statement. That's a bug for sure.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63