C ++ indicates doubt

I have doubts about this code that I saw at the university.

struct nodeList{ int data; nodeList * next; }; typedef nodeList * List; void filter( List &l ) { List * aux = &l; while(*aux) { if( (*aux)->data > 3 ) { List toDelete = *aux; *aux = (*aux)->next; delete toDelete; } else { aux = &((*aux)->next); } } } 
  • I do not know what List * aux = &l; does . Since List same as nodeList * , so the code will be nodeList * * aux = &l; , and this is actually what I don’t understand, is that a pointer to a pointer that contains the address of a nodeList struct pointer

  • The second problem I encountered is the last line. aux = &((*aux)->next); Why aux without * on the left side? if it was declared as List *aux = &l; is List just a pointer to the first node?

Thanks in advance. I googled a lot, but I did not find the answers. If you can answer my questions, I will be very grateful.

+6
source share
3 answers
  • That's right.

  • You must always match data types. aux is nodeList ** , so any assignment must have the same data type. Since ((*aux)->next) is nodeList * , you use the & operator to get the nodeList ** .

Datatypes

Variables have certain data types. For example, aux is List* , but List is an alias of nodeList* , so aux is nodeList** .

But also expressions have data types in general. For instance,

  • the expression ((*aux)->next) has a nodeList * data type and &((*aux)->next) has a nodeList ** data type. You use the & operator to get the memory address of a variable, and the data type of the result of using & is another star.
  • the expression *aux is of the nodeList * data type because aux is nodeList ** , and the star operator gets the value of the pointer element with the pointer, effectively removing one star from the data type.
+5
source
 void filter( List &l ) 

This means "pass by reference" and not "by value", i.e. pass a pointer to an object l, rather than copying l to the stack. In terms of how it works on the computer, there is no difference between "void filter (List & l)" and "void filter (List * l)", since both of them become pointers passed to the function. From the encoder's point of view, however, "void filter (List & l)" has the advantage that complier ensures that you do not get "nullptr".

 List * aux = &l; 

This means "give me a pointer to an object l". The "&" symbol is used for many different things in C / C ++, and in this context it means "give me the address." aux is a pointer to an object of type List, not an object of type List (of course, List itself is a pointer to a nodeList here).

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

*aux is the object that aux points to, which is "nodeList *". (*aux)->next is the next pointer in the nodelist object pointed to by the List object pointed to by aux. aux = & sets the aux pointer to point to this object.

This code segment is not particularly clear or concise, so I assume that it is written this way as an educational tool to understand if you understand pointers, links, and an address in C / C ++. Thus, perhaps you should study the definitions of these operators in a tutorial on pointers to C / C ++.

+2
source
  • I do not know what List * aux = &l; does actually. Since List same as nodeList * , so the code will be nodeList * * aux = &l; , and actually I don’t understand what a pointer to a pointer containing the address of a nodeList struct pointer?

It takes the address of the equivalent of the pointer from the referenced parameter (thus the same as nodeList** ).

  • The second problem I find it difficult to understand is the last line. aux = &((*aux)->next); Why aux without * on the left side? if it was declared as List *aux = &l; is List just a pointer to the first node?

aux is a reference, so the original pointer passed as parameter l will be changed.

+1
source

Source: https://habr.com/ru/post/972889/


All Articles