Please look at C++ Overview of pointers in , This is just an expansion

Array pointer

In fact, the main point here is c++ Pointer operations in

Array element pointer :
A variable has an address , An array contains several elements , Each array element has a corresponding address ,
Pointer variables can point to array elements ( Put the address of an element in a pointer variable )
The so-called pointer of array element is the address of array element
You can use a pointer variable to point to an array element int a[10]={1,2,3,4,5,6,7,3,2,3}; int *p; p=&a[0]; Equivalents and p=a; Equivalents and int *p=a; Equivalents and int*p=&a[0] Be careful :
1) Array name a It doesn't represent the entire array , Address representing only the first element of the array
p=a The role of is " hold a The address of the first element of an array is assigned to a pointer variable p", instead of " Put the array a The value of each element is assigned to p" Array pointer :
A pointer to an array element The function of array pointer :
Use an array pointer to indirectly access the elements of an array Definition of array pointer :
int *p; Initialization of array pointers ; int a[4] ={1,2,3,4};
int *p = a;// Array pointer , A pointer variable is defined p The first address of the assignment array ( The address of the first element ),P Point to the first element of the array int *p = &a[0];// Equivalent to the above sentence How an array pointer accesses the elements of an array :
1) p+1 Represents the next element pointing to the array
2) p-1 Point to the last element of the array myth :
Traversing an array with an array pointer
for(int i=0;i<4; i++){
} // Mistakes in learning : Array name a Is a constant Equivalents and *(10++)
// Array name ++ This is wrong
printf("*a=%d\n",*a++);// This writing is wrong */

Reverse array : Transpose the contents of the array back and forth

void nixuArray(int a[],int len){
int i=0,j=len-1;
// Modify subscript

One dimensional pointer array

 The definition of pointer array :
data type * Array name [ The length of the array ];
int* pa[3];
// Define an array of pointers , The array name is pa, It can be used to store 3 A pointer to the ( Must be int Pointer to type variable );
The use of pointer arrays :
int a=3,b=4,c=5;
int *pa[3] ={&a,&b,&c};
pa[0] a The address of
pa The first address of the array , Variables again a The address of
// Use array names to access a Value
int a1[2][2]={1,2,3,4};
// Defines an array of pointers , The assignment is a1[0] Is the pointer to the first line 
int *pa1[2]={a1[0],a1[1]};
printf("**pa1=%d\n",**pa1); //*pa1 It's a pointer array pa1 The value of the first address in = a1[0]
// meanwhile a1[0] = a1 The first address
*pa1 = a1[0] printf("&a1[0] = %d\n",a1[0]); printf("*pa1 = %d\n",*pa1); // Forward thrust
// therefore **pa1 namely *(*pa1)=*(*pa1[0])=*(a1)=*(a1[0])=*(&(a1[0][0]))=a1[0][0] =1;
//**(pa1+1)=*(*(pa1+1))=*(*pa1[1])=*(a1[1])=*(&(a1[1][0]))=a1[1][0]=3 // Backstepping
// So I want to take the numbers 2 When Equivalent to
// Take the numbers 3

Array names access two-dimensional arrays

 The formula *(*(a+i)+j) == a[i][j]

Using a normal pointer to access a two-dimensional array

// Although this writing method can also get the elements in the two-dimensional array , But it's not standard to write like this , Using the mechanism of memory storage

The way data is stored in memory , From the top address down

int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
int *p=a
for(int i=0;i<12;i++){
printf(" %d \n",*(p+i));

Two dimensional array pointer

 General form :
data type (* Pointer variable name )[ Number of columns in a two-dimensional array ];
among " Type specifier " Is the data type of the selected index group . “*” The variables that follow are pointer types To assign a two-dimensional array to a pointer , It should be assigned as :
int a[3][4];
int (*p)[4]; // This statement is to define an array pointer , Point to include 4 One dimensional array of elements
p=a; // Assign the first address of the two-dimensional array to p, That is to say a[0] or &a[0][0]
p++; // After the statement has been executed , That is to say p=p+1; p Across the line a[0][] Point to the line a[1][] So array pointers are also called pointers to one-dimensional arrays Traversing elements of a two-dimensional array int a[3][4]={1,3,4,5,6,7,8,9,10,13,15,17}; int (*p)[4]=a; So the previous traversal of Array a Can be replaced with p :*(*(p+i)+j)

Pointer array and two-dimensional array pointer variable difference

 int *pa[3]={&a,&b,&c}; pa It's an array of Pointers 
int *pa1[2]={a[0],a[1]}
int (*pa)[3] ;// Two dimensional array pointer The difference between pointer array and two-dimensional array should be injected , Although both can be used to label two-dimensional arrays , But its expression and meaning are different
A two-dimensional array pointer variable is a single variable , In its general form (* Pointer variable name ) The brackets on both sides are indispensable , The pointer array type represents multiple pointers
The needle ( A set of ordered pointers ) In general form "* Pointer array name " There can't be brackets on both sides .
for example :
int(*p)[3]; // Represents a pointer variable to a two-dimensional array , The number of columns of the two-dimensional array is 3 Or the length of the array decomposed into one dimension is 3
int *p[3]; // Express p It's an array of Pointers , There are three subscript variables p[0],p[1],p[2] Are pointer variables

String pointer

 char * Variable name =" String content " // " String content " Is a constant 
// When pointing to '' In single quotes , For character pointers "" Double quotation marks are string pointers

The difference between string pointers and character arrays

 // String pointer The direction can be changed 
char *ss="abc"; ss="bcd"; // A character array
char s1[]="abc";
s1="hello" This is wrong You can change the content inside, but you can't point to it again

Pointer function

 The return value type of a function is a pointer , We call it pointer functions 
Format :
Type specifier * Function name ( Formal parameter table ){
The body of the function
} example :
// Returns the address of the largest of two numbers
// What is returned is a formal parameter x and y The address of the middle large number
int * max( int x,int y){
printf("x= %p\n",&x);
printf("y= %p\n",&y);
return x>y?&x:&y;

A function pointer

 A function pointer 
stay C In language , A function always occupies a contiguous area of memory , The function name is the first address of the memory area occupied by the function . We can put the first address of the function ( Or entrance address ) Give a pointer variable , Make the pointer variable point to the function . Then you can find and call this function through the pointer variable . We call this pointer variable to a function " Function pointer variable " Function pointer definition method :
The general form of function pointer variable definition is :
Type specifier (* Pointer variable name )( The parameters of the function );
among " Type specifier " Represents the type of the return value of the referred function
"(* Pointer variable name )" Express "*" The following variables are defined pointer variables Declaration of functions : int sum(int a,int b);-----> A function pointer int (*p)(int a,int b); // Defines a function pointer p
//p Can store the return value is int type , And there are two formal parameters , The types of formal parameters are also int The address of a function of type // Function pointer initialization
p = sum;//sum Stored is the first address of the function in memory // When defining function pointers , You don't need to write the parameter name
int (*p)(int int); The use of function pointers 1. Define function pointers
int(*p) (int int);
2. Initialize function pointer
3. Use function pointer to call function indirectly
int s=p(23,45); The use of function pointers : for example + - x / when , Just judge the symbol , then p = sum / p = jian Can

C More about pointers in languages

  1. C Arrays of pointers in languages

    C What is an array of pointers in a language , image char *a[]={"ddd","dsidd","lll"}; Here's a note. If we use a That's the first element ...

  2. 【ZZ】C Pointers and memory leaks in languages &amp; Write efficient C Procedure and C Code optimization

    C Pointers and memory leaks in languages This paper discusses several kinds of traps that can be avoided when using dynamic memory allocation ...

  3. GO The pointer in language Go Pointer syntax and C++ In line , It's all used * As a symbol , Although grammatically close to , But the actual difference is not small . Go Use var Defining variables : var v6 *in ...

  4. C Pointer notes in language

    C Language pointer Get the address of the variable have access to & Operator to find the location of the variable in memory int x = 1; printf("x The memory address of is "%p\n",&x) %p Format ...

  5. C The offset of pointer addition and subtraction in language

    C Language pointer shift technique ( It's also a pit to pay attention to ) - Chen Jiezhu's blog - CSDN Blog C In language ...

  6. C Pointers and memory leaks in languages

    Introduction for any use C Language people , If you ask them C What's the biggest trouble with language , Many of them might say pointer and memory leaks . These are really things that cost developers most of their debugging time . Pointer and memory leaks seem daunting to some developers ...

  7. C Pointers and memory leaks in languages

    introduction For any use C Language people , If you ask them C What's the biggest trouble with language , Many of them might say pointer and memory leaks . These are really things that cost developers most of their debugging time . Pointer and memory leaks seem daunting to some developers , however ...

  8. C Pointer learning in language ( Small blackboard )

    Pointer is C The essence of language , It's also C The danger of language is , Today, I went over it again C Language , I made a summary . Welcome to read . (1) The meaning of a pointer is also a data type , It's a variable that points to the address . for example : { int a = 10; ...

  9. [C]C Pointer and memory leak in language

    introduction Original address :, Reprint please indicate the source address . For any use C Language people , If you ask them C Linguistic ...

  10. On C The difference between a two-level pointer and a two-dimensional array

    Just beginning to learn C When it comes to language , I think an array can define a first level pointer to access , I think I can define a secondary pointer to access a two-dimensional array . Obviously this is wrong . Let's see C The way arrays of languages are stored in memory . actually C Arrays in languages , ...

Random recommendation

  1. Solve the problem rails Use activerecord Report errors NameError: uninitialized constant ActiveRecord::Migrator::Zlib

    Context The original system is 15.10, But I only support 1 year , So upgrade today 16.04, Run after the environment is ready rake migratte Report errors task :default => :migrate desc 'Run m ...

  2. Map Place the class pointer in and implement the call

    Multi process communication is used in the work , Used to map as well as multimap To implement . Need to do a simple test example , Go straight to the code . /* * main.cpp * Created on: Oct 28, 2013 * Au ...

  3. core-- Thread pool

    For servers - Client software with this architecture , Usually the data of the client comes from the server , How to make a server process , To satisfy the data requests of multiple client programs ? One simple way is to , Every time a customer requests to pick up , The server creates a thread for the client . When there is 10 ...

  4. Java(Android) Notes on programming ideas 02: Combination and inheritance 、final、 Policy design pattern and adapter pattern 、 Inner class 、 Serialization control ( matters needing attention )

    1. The choice between composition and inheritance Both composition and inheritance allow children to be placed in new classes , Composition is done explicitly , Inheritance is implicit . Composition techniques are often used when you want to use the functionality of an existing class in a new class rather than its interfaces . That is to embed an object in a new class , Let it be ...

  5. hdu 6068--Classic Quotation(kmp+DP)

    Topic link Problem Description When online chatting, we can save what somebody said to form his ''Classic ...

  6. Git Learn to use

    1. Register code cloud and set up remote warehouse 2. install git 3. Use eclipse egit Push and Clone Establish local warehouse , The success is as shown in the figure Push project to local warehouse and remote warehouse Using the right-click menu team-share Options , Associated with the warehouse ...

  7. [STM32F103]PWM Input capture configuration

    l  The initialization timer corresponds to the channel IO The clock of . l  initialization IO mouth , The mode is input : GPIO_Init(); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 ...

  8. nodeJs Use express-http-proxy Forward the request

    It is often used in the development process nodeJs Do forwarding layer Use express coordination  express-http-proxy Can easily complete the forwarding Using process : install  express-http-proxy npm inst ...

  9. Python Delete files on a regular basis 、 Organize the folder

    1. According to the parameters passed in , File directory , Regular expressions matching files , The number of days expired is deleted , These can be written in the configuration file del_file.conf. #!/usr/bin/env python # en ...

  10. Dapper Even the table query

    Entity class : UserInfo: public partial class UserInfo { public UserInfo() { this.Persion = new HashSet<Per ...