The type of the pointer and the type to which the pointer points:

  • The type of a pointer is what remains after the name of the pointer is defined
  • The type to which a pointer points is defined with an * and the pointer name removed
int *ptr; // The pointer is of type int *
char *ptr; // The pointer type is char *
int **ptr; // The pointer is of type int **
int (*ptr)[3]; // The pointer is of type int(*)[3]
int *(*ptr)[4]; // The pointer is of type int *(*)[4]

int *ptr; // The type to which the pointer points is int
char *ptr; // The type to which the pointer points is char
int **ptr; // The type the pointer points to is int *
int (*ptr)[3]; // The type to which the pointer points is int()[3]
int *(*ptr)[4]; // The type to which the pointer points is int *()[4]
Copy the code

The pointer refers to: is an address, depending on the number of different computer lengths & is an address operation, * is a value operation

int *p;
int a=10;
p=&a;//p is the pointer variable name assigned to an address value
Copy the code

The array name is actually a pointer

int arr1[3] = {1.2.3};
// Get arr1[0], arR1 [1]
*arr1;
*(arr1+1);
Copy the code

Structure and pointer relationship

Struct MyStruct  
{  
int a;  
int b;  
int c;  
}  

MyStruct ss={20.30.40};// declare the structure object ss and initialize the three members of ss to 20,30, and 40.
MyStruct *ptr=&ss;// declare a pointer to the structure object ss. Its type isMyStruct star, it points to a type of MyStruct.int *pstr=(int*)&ss;// declare a pointer to the structure object ss. But its type and the type it points to are different from PTR.How to access three member variables of SS through pointer PTR? Answer: the PTR - > a; ptr->b; ptr->c; How to access three ss member variables through the pointer PSTR? Answer: * PSTR;// Accesses member A of ss.
*(pstr+1);// Accesses member B of ss.
*(pstr+2)// Access member C of ss.
Copy the code

However, using the second method to access structure members is risky. Arrays can be used this way because arrays are open contiguous memory, while structures are not. All C/C++ compilers arrange the elements of an array in contiguous storage with no space between them. When storing individual members of a structured object, however, word-alignment, double-word alignment, or other alignment may be required under certain compilation circumstances, and “padding bytes” may be added between adjacent members, resulting in a gap of several bytes between each member.