Knowledge summary of "level two pointers" for the initialization of chained storage for linked lists

Source: Internet
Author: User

First explain the "level two pointer":

A first-level pointer is associated with its value (an address) in the name of the data, the data can be any type and for any purpose, but the two-level pointer to the data associated with only one type of use, is the address.

Pointers are two purposes: to provide read or overwrite of the target, then the level two pointer is to provide a read or overwrite of the memory address.

The representation of a pointer is the address, the core is the pointer to the relationship, and the function of the operator "*" is to access the object pointed to by the relationship. If there is a pointing relationship to B, A is the address of B, and "*a" means indirectly accessing B through this pointing relationship. If the value of B is also a pointer to C, then B is the address of C, and "*b" represents an indirect access to C, if C is a variable of type integer, real, or struct, or the type of data that holds these types.array Elements, then B (that is, the address of C) is a normal pointer, called First -level pointers,The variable used to hold the first-level pointer is called first-level pointer variable 。 A (that is, the address of B) is a pointer to a pointer, called Level Two pointers, a variable for holding a level two pointer is called Two-level pointer variable. If a, B, C are variables, that is, C is a normal variable, B is a levelpointer Variable, where the address of c is stored, A is a two-level pointer variable, which holds the address of B, then these 3 variables occupy their own storage unit in memory respectively. The above content from Baidu Encyclopedia。The first time you encounter a "level two pointer" in the Learning Data Structure List section, the code is as follows:
typedef struct NODE
{
DataType data;
struct Node *next;
}listnode,*linklist;

void Initlist (linklist *head)//Head is a level two pointer

{
if ((*head) = (linklist) malloc (sizeof (ListNode))) ==null)
exit ( -1);
(*head)->next=null;
}
The book explains that head is a pointer to the head node of a single-linked list that receives the address of the head-pointer variable in the main program to initialize a single-linked list, and *head the head-pointer variable that corresponds to the single-linked list in the main program.
read a few times do not understand, Baidu a bit:

There is no reference in the C language, so you must use a level two pointer when trying to change the value of a pointer.

Reference types can be used in C + + to implement.

Demonstrate an example of the error if the above initialization code is written like this:

void Initlist (linklist head)

{

Exit (-1);
(head)->next=null;
}
When this code is executed, the header pointer in the main program is not processed because the parameter is only a copy of the argument and the operation on the parameter does not affect the argument.
Let's look at a simple example when formally explaining the Initlist initialization code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Memorylocate (char **ptr)
{
*ptr= (char *) malloc (10*sizeof (char));
}
int main (int argc,char **argv)
{
Char *buffer;
memorylocate (&buffer);
strcpy (buffer, "12345");
printf ("Buffer%s\n", buffer);
return 0;
}

Operation Result:buffer 12345

If you change to

void Memorylocate (char *ptr)
{
ptr= (char*) malloc (10*sizeof (char));
}
int main (int argc,char **argv)
{
Char *buffer;
memorylocate (buffer);
strcpy (buffer, "12345");
printf ("Buffer%s\n", buffer);
return 0;
}

strcpy can't execute ... Description buffer not allocated to storage space ...

In this way, the initial explanation of how to explain why using the two-level pointer will be enlightened.

Knowledge summary of "level two pointers" for the initialization of chained storage for linked lists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.