Review-pointer, second-level pointer, linked list, review pointer
Level 1 pointer
- Int * p; // defines an int type (4 bytes) pointer p
- & P // represents the address location of p.
- P // indicates the address location pointed to by p (that is, the value of the p variable)
- * P // indicates the content in the address pointed to by p
So *'s role:The value of the p variable acts as the address to access the content of this address.
Level 2 pointer
- Int ** pp // defines an int * pointer pp
- & Pp // address location of the pp
- Pp // indicates the address pointed to by pp, for example, pp = & p. Note that pp is of the int Type X. Therefore, an error will be reported if pp is directed to another type of address,
- * Pp // indicates the content on the address pointed to by pp. For example, if pp = & p, * pp will access the content of & p.
- ** Pp // indicates that the content on the address pointed by pp is used as the address to access the content of this address. * pp = * (* pp) = * p
For example:
Int main () {int I = 5; // suppose & I = 0x100 int * p = & I; // assume & p = 0x500 int ** pp = & p; // assume & pp = 0x1000/* So & I = 0x100, I = 5 & p = 0x500 p = 0x100 * p = 5 & pp = 0x1000 pp = 0x500 * pp = 0x100 ** pp = 5 */}
Linked List
First, let's talk about malloc and free:
This function is required when you add or release a linked list to prevent content from being automatically released.
void *malloc(int size);
The return value of malloc is of the void * type. Therefore, you must use a pointer to obtain the value and use a forced conversion function.
void free(void *ptr);
Release address, used with malloc, for example:
Char * p; char str [] = "ABCD"; p = (char *) malloc (strlen (str); // the pointer must be used for obtaining, and forcibly convert strcpy (p, str );...... free (p );
An example of a two-way linked list is as follows:
# Include "stdio. h "
# include <stdlib. h>
# include "string. h "
typedef struct NAME {
char * name;
struct NAME * preced;
// the previous name linked list member struct NAME * next;
// The member of the next name linked list
}
T_NAME, * PT_NAME;
PT_NAME g_ptNameListHead = NULL;
// void add_LinkList (PT_NAME nameNew) {
PT_NAME ptTamp;
int I = 0;
If (g_ptNameListHead = NULL)
// Add the linked list {
g_ptNameListHead = nameNew;
return;
} else {
ptTamp = g_ptNameListHead;
while (ptTamp-> next)
for the first time)
// whether the members of the next linked list are empty {ptTamp = ptTamp-> next;} ptTamp-> next = nameNew;
// Add the members of the next linked list nameNew-> preced = ptTamp; // Add the return;
}
void add_name (char str []) {
PT_NAME ptTamp;
char * p;
p = (char *) to the next linked list Member *)
malloc (strlen (str);
strcpy (p, str);
ptTamp = (PT_NAME) malloc (sizeof (T_NAME);
ptTamp-> name = p;
ptTamp-> preced = NULL;
ptTamp-> next = NULL;
add_LinkList (ptTamp );
// Add a linked list}/* locate the name Position */
PT_NAME find_name (char str []) {
PT_NAME ptTamp;
if (g_ptNameListHead = NULL)
return NULL;
PtTamp = g_ptNameListHead;
while (ptTamp) {
if (strcmp (ptTamp-> name, str) = 0)
// find {
return ptTamp ;
}
else
ptTamp = ptTamp-> next;
} return
NULL;}
int del_name (char str []) {
PT_NAME ptTamp;
PT_NAME ptLast;
PT_NAME ptNext;
PtTamp = find_name (str);
if (! PtTamp) return-1;
if (ptTamp = g_ptNameListHead)
// if the chain table header is removed {
g_ptNameListHead = ptTamp-> next;
// point to the next linked list member
} else {
/* Get the previous linked list member and the next linked list member */
ptLast = ptTamp-> preced;
ptNext = ptTamp-> next;
/* remove the current linked list member */
ptLast-> next = ptNext;
ptNext-> preced = ptLast;
}
free (ptTamp-> name);
free (ptTamp);
return 0 ;
} void List_name (void) {
int I = 0;
PT_NAME ptTamp = g_ptNameListHead;
while (ptTamp) {
printf ("<% d> % s \ n", I, ptTamp-> name);
ptTa Mp = ptTamp-> next ;
}
} void scanf_name (char cmd) {
char name [128];
switch (cmd) {
case 'A ':
// add {printf ("please enter name: \ n");
scanf ("% s", name);
add_name (name );
printf ("add % s OK \ n", name);
break;}
case 'D':
// del {printf ("please enter name: \ n ");
scanf ("% s", name);
if (del_name (name) <0) {
printf ("del % s error \ n", name );
}
else printf ("del % s OK \ n", name);
break;
} case 'l':
// list List_name ();
break;
} Int main (int argc, char ** argv) {
char c;
while (1) {
printf ("*********************** \ n ");
printf ("<l> List all the names \ n");
printf ("<a> add one name \ n ");
printf ("<d> del one name \ n");
printf ("<q> quit \ n ");
printf ("*********************** \ n");
do {
scanf ("% c ", & c);
} while (c! = 'A' & c! = 'D' & c! = 'Q' & c! = 'L');
switch (c) {
case 'A':
case 'l':
case 'D':
scanf_name (c);
break;
case 'q ':
return 0;
default:
break ;}}}