Summarized some common pointer error-prone issues (4) and pointer Common Problems
Pointer and struct
Introduction: We can use the structure of C to represent data structure elements, such as linked lists or Tree nodes. Pointers are links that link these elements together.
Typedef struct _ person {char * firstName; char * lastName; char * title; unsigned int age;} Person;
/* Initialize with dot notation */
Person person;
Person. firstName = (char *) malloc (strlen ("Emily") + 1 );
Stcpy (person. firstName, "Emily ");
Person. age = 23;
/***** Struct pointer initialization */
Person * ptrperson;
Ptrperson = (Person *) malloc (sizeof (Person ));
Ptrperson-> firstName = (char *) malloc (strlen ("Emily") + 1 );
Strcpy (ptrperson-> firstName, "Emily ");
Ptrperson-> age = 23 ;//(* Ptrperson). age = 23;
Allocate memory for the struct. The allocated memory size is at least the sum of the lengths of each field. However, the actual length is greater than the sum, and the fields of the struct may be filled.The elements in the struct array are filled.
Structure release problems:
Use struct variables and pointer function parameters pointing to struct
1. Use the member of the struct variable as the parameter. (The usage is the same as that of common variables)
2. Use struct variables as real parameters. The parameter must also be a struct variable of the same type. The parameters also occupy the memory during the call. (Large space and time overhead). This method is rarely used.
3. Use the pointer pointing to the struct variable (OR array) as the real parameter, and pass the address of the struct variable (array) to the form parameter.
Use pointers to process linked lists
A linked list is a data structure that dynamically stores and distributes data. A linked list is a commonly used data structure in C programming. For example, to create an integer linked list, it is generally defined as follows:
struct int_node { int val; struct int_node *next;};
Assume that there are five students whose scores for A course are A, B, C, D, and E, the addresses of these five data storage units in the memory are 1248, 1488, 1366, 1022, and 1520 respectively. Their linked list structure is as follows.
The linked list has a "head Pointer" variable, which is represented by a head in the figure. It stores an address that points to the first node in the linked list, and the first node points to the second node ...... Until the last node. This node no longer points to other nodes. It is called "Table tail". Its address section stores a "NULL" (indicating "Empty address"), and the linked list ends here. Each node in the linked list contains two parts: the actual data required by the user and the address of the next node.
Each node in the linked list has only one pointer to the next node. The linked list is called a single-chain table. In fact, there can be more than one pointer used to link other nodes. If each node has two pointers used to link other nodes, one pointing to the forward node (called the forward pointer) and the other pointing to the next node (called the next pointer), a two-way linked list is formed. Two-way linked list.
/* Example of a single-chain table (static linked list: because all nodes are defined in the program, they are not opened temporarily and cannot be released after use) */# include <stdio. h> // # define NULL 0 struct student {int num; int score; struct student * Next;}; int main () {struct student a, B, * head, * p;. num= 10012;. score = 99; B. num= 10013; B. score = 81; head = & a;. next = & B; B. next = NULL; p = head; do {printf ("% 5d % ld \ n", p-> num, p-> score); p = p-> Next ;} while (p! = NULL );}
Q: Can variable names of struct be assigned to pointers as addresses? Is there no head pointer? What role does p play? Can I leave it alone?
Functions used to process dynamic linked lists: calloc/malloc/free
Malloc function prototype: void * malloc (unsigned int size); it is used to allocate a continuous space with a length of size in the dynamic storage area of the memory. The value (return value) of this function is the starting address of an allocation domain (void type). If the function is not successfully executed, a NULL pointer is returned ).
The prototype of the calloc function: void * calloc (unsigned n, unsigned size). It is used to allocate n continuous spaces with a length of size in the dynamic memory storage area. The function returns a pointer to the starting position of the allocation field. Otherwise, NULL is returned.
Free function prototype: void free (void * p); releases the dynamic storage area directed by p. Free has no return value.
Creating a Dynamic Linked List)
/* Write a function to create a one-way Dynamic Linked List with three student data */# include <stdio. h> # include <malloc. h> // # define NULL 0 # define LEN sizeof (struct student) struct student {long num; float score; struct student * next;}; int n; struct student * creat (void) {struct student * head; struct student * p1, * p2; n = 0; p1 = p2 = (struct student *) malloc (LEN ); scanf ("% ld, % f", & p1-> num, & p1-> score); head = NULL; while (p1-> num! = 0) {n = n + 1; if (n = 1) head = p1; else p2-> next = p1; p2 = p1; p1 = (struct student *) malloc (LEN); scanf ("% ld, % f", & p1-> num, & p1-> score);} p2-> next = NULL; return (head) ;}int main () {creat ();}
To create a linked list, you must first define a structure type that contains the data and pointer fields, then create a header pointer head pointing to the header node, and then dynamically apply for a memory as the header node through the malloc function.
Typedef struct node {int data;/* information */struct node * link;/* pointer */} NODE;/* define NODE */node * head; /* define the head pointer */
After defining the structure type and header node, you can create a header node that does not contain data by following the following statements.
NODE * p;/* Indicates a pointer variable to the NODE p */p = (NODE *) malloc (sizeof (NODE )); /* Header node of the Application Form */p-> link = NULL;/* set the link of the header node to NULL */head = p;/* head to header node p */
Because the linked list has only one header node and no data node, it is called an empty linked list.
P = (NODE *) malloc (sizeof (NODE);/* apply for a data NODE */to save data in the linked list, you can insert data nodes from the header to the linked list. For example, you can insert a data node:
P = (NODE *) malloc (sizeof (NODE);/* apply for a data NODE */gets (p-> data ); /* enter a new data */p-> link = head-> link;/* create a link. Store the link of the header node into the link of p */head-> link = p;/* Insert the data node into the header node and become the first data node */
Insert the first data node to the linked list, and then insert the next data node.
Create (NODE * head, int n) Based on the above linked list creation process, you can write the create FUNCTION to create a linked list with n data nodes.
create(NODE *head,int n) { NODE *p; for(; n>0;n--) { p=(NODE*) malloc(sizeof(NODE)); if(p==NULL) exit(0); gets(p->data); p->link = head->link; head->link = p; } }