Data Structure - Pointer

pointer

concept:

(1) p is a pointer / address variable (the memory address is a variable), which is the value (p stored) the address of a variable.

p received (into the) address which variable, which points to a variable. The variable become the object of p.

(2) * p value of p is pointed object.

→ * p is a value, the value of the variable to point

 

in principle

(1) * behind only indicative parameters with the address, * p

(2) & behind with only variable. & A parameter indicating the address of a

(3) '=' is the assignment by default only allow the same type of parameter assignment (if the assignment will be the same, but the results will be wrong)

→ only, (pointer) assigned to address (pointer) Address

(Variable) values ​​assigned to the (variable) values

 

definition

Defines base type pointer Elemtype * elem;

(ElemType unit content type is the pointer points)

C language is not specific string type, we usually put the string in an array of characters

 

When the assignment is defined: int * p = & a (√) (p initialization address stored as & a)

Corresponds int * p, p = & a;

(I.e. ★★★ defined int * p to be understood that when the base address of the need to assign a variable address, and after a defined value * p, the need to assign values)

Because the pointer p does not point to a variable, we need to assign an address

 

Similarly int * p = a; ×

Not the value (integer variable) assigned to the address (variable)

Corresponds int * p; p = k; (×)

 

★★★ address assignment & k

int *p,*q,a=20; 

 

① * p = a; √ pointer p to an address assignment

(All values ​​will be achieved * p value assigned to the variable pointed to a)

 

②q = p; √ (because p / q is a pointer, the assignment statement to achieve the value of p to q)

③p = q = & a; √ (p / q (value) of the address, & a is the address)

④p = * q; × p = a; × (p is an address, * q, a is a value)

⑤a = * p; * p = * q; √ (all values)

 

Values ​​(structural type of pointer)

If the definition of a pointer to a specific data type variable

The elm * p = stu1;

(Equivalent to elem * p; p = & stu1;)

Stu1.name equivalent to the (* p) .name

And equal to p → name: name referring to a structure variable p member (node) (the value)

 

 

Array Pointer (i.e., the array is an array of pointers point to a pointer, the pointer is stored in the base address of the array)

When a pointer variable is initialized to the name of the array, said pointer variable that points to the array (p stored in the first address str)

The char str [20], * p; p = str;

Can change the character values ​​of the array (writable)

 

Char pointer pointing

char * str = "I love China!";

Or (* arbitrary position as long as the line in the middle)

char * str;    str = "I love China!";

Point to a fixed constant string (address of the first character string, i.e., the first address) is not writable read only

Char str pointer to make a command input string, the string will be all the constant output. (Printing the first character will automatically print the complete string)

 

★ character array pointer and the pointer difference

(1) char * p (a character defined pointer)

(2) ①p = array name; (array address / address that is assigned to the first array pointer)

p = String; (string address / address is assigned to the first character of the character pointers)

 

 

★★★

* (P + n), p [n] is the first (i 1 +) value of the elements

 

Different ★★★ C / C ++ pointers

Export

printf ( "% s", p); → output string (character string pointed to by p)

printf ( "% d", p); → output pointer value p

 

C++ 

Output:

cout * p → output string (memory pointer)

cout p → output pointer stored in the address

 

 

Function pointer

Is a pointer to function variables.

Return Type (* function pointer name) (primitive parameter)

Assignment: a function pointer to a function name = name;

Call: function pointer name (parameters);

 

The pointer variables can be used to call the function.

 

Command and ordinary variable functions to be performed are the same as in memory of

Since there will be an address in memory

The first command function start no exception

And its start address is called the address of the function

Now with a pointer to this address

When indirect access to this command pointer stored address, this function is activated

 

This pointer is called the function pointer

"Function pointer" actually means "pointer to function"

"Function pointers" is a class of functions. What class do? Is a function that returns a pointer

In fact, a good memory

"Xx function" is the return value is a function of "xx".

 

 

 

 

 

 

 

 

Published 46 original articles · won praise 15 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_41850194/article/details/91873147