pointer, reference

Correctly define pointer variables:

After the definition, the pointer variable should be assigned a value before it can be used, that is, use p=&a;

 
 
int main() {
int a=5;
int *p;
p=&a;
  return 0;
}
mistake:

Use without assigning a value to p! *p refers to the contents of the variable pointed to by p.

int main() {
int a=5;
int *p;
*p=a;
  return 0;
}

Linked list:

In the C language, the linked list is generally represented by a pointer to the head node. This head pointer is the entry and starting point of the linked list, and also represents the entire linked list. So, what is it in essence, in the final analysis, it is nothing more than a pointer variable. In C++, you can also define a class called SingleList to represent a singly linked list class, and a specific linked list instance is the specific object of this class. At this time, what is the essence of the linked list? is an object of class.

References in the linked list:

When implementing a linked list in C language, there is usually an init() function. Its function is to initialize the linked list. The simple action is to set the head pointer of the linked list to NULL, and an empty linked list is constructed. Therefore, the entry parameter of the function must have the head of a linked list, which can only be set to NULL in the function. Suppose, we write like this,

void init(Node *head)
{
head = NULL;
}
When called in the main function,
int main(void)
{
Node *head;
init(head) ; /* Initialize an empty linked list */
return 0;
}
Above After the commented code is executed, does head really become NULL? isn't it? You think, Node *head; In this sentence, head is now a random value. After passing it to the init function, although it is changed to NULL inside the init() function, it modifies the local variables in init, only the formal parameter variables That's it, in fact, after the function is executed, the head in the main function does not change. This makes it impossible to initialize an empty linked list.
A feasible solution is to use references. Defined as the following
void init(Node * &head) or when using a pointer to a pointer void init(Node **phead)
to use the reference, use init(head) in main. When using a two-dimensional pointer, the main should be written as init(&head);


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324610597&siteId=291194637