In C language, any variable has two meanings:
(1) The address of the storage unit representing the variable; the address of the variable lvalue
(2) Represents the value of this variable; rvalue
There are only two cases for access to a variable:
First, write a value to the address of the variable (lvalue)
The second is to take the value of the variable from the address of the variable (rvalue)
How to access objects
Direct access: Access through object name
like:
int a; a = 1024; b = a;
Note: Direct access is restricted by scope
Indirect access:
Access through the object's address and pointer access.
As long as you know the address of the object, you can access it anywhere.
Not subject to scope limitations.
What is a pointer
The address of the memory unit (such as: memory memory):
The memory unit assigned to each object has a number, which is what we say
The address of the storage unit. And the storage units are numbered in bytes.
In C language, the concept of a pointer is similar to that of an address, and you can directly think that a pointer is an address.
The address of a variable, which we also call a "pointer" to a variable
& Take the earth and hold the symbol
Monologic operator, "take the address of the xxx object"
To access an object through a pointer, we must first solve the problem of saving the object's pointer (address).
Another variable needs to be defined to save its address. We call this variable pointer variable.
Pointer variable
A pointer variable is also a variable, which is also used to save data, but the data saved by the pointer variable is the address of other objects.
Definition of pointer variables:
The type of object to be pointed to * Pointer variable name;
"Type of object pointed to":
The type of the object to which the pointer variable points, not the type of the pointer.
like:
int *p; int a=10; p=&a;//p saves the address of object a // p points to a
Pointer-related operators
& : Address character
int a;
&a: Indicates the address of object a
The address of object a is actually the pointer of object a.
*: Pointing operator , monocular operator
*Address (*Pointer): The variable (object) corresponding to the address.
like
int a=10; int *p=&a; int b=*&a;//b=10,int b=a
So *(&a) <=> a
*& can be directly cancelled (lexical analysis of the compile principle state machine)
Pointer variable as function parameters
The value passed is still the one!! But this "value of the actual parameter" may be the address of a certain object!
In C language, the transfer of function parameters can only be "value transfer".
Formal parameter = value of actual parameter
Wild pointer:
void XXX2(int * m, int * n) { int * t;//Define a pointer variable t, but t is not assigned //t has no assignment, but it does not mean that it has no value. On the contrary, it will have a corresponding //I don't know what this value is undefined *t = *m; //Operating field pointers may cause segfault (write operations on an unknown space) *m = *n; *n = *t; //Operate field pointer,May cause Segment error(Read an unknown space)
*t is placed on the left side of the '=' symbol, representing the lvalue pointing to the object
Write operations to the object pointed to by t
*t is placed to the right of the ‘=’ symbol, representing the right value of t pointing to the object
Read the object pointed to by t
So t is a "wild pointer", and we don't know whether the memory space pointed to by t is readable and writable.
If it is unreadable or unwritable, then subsequent operations on *t will lead to illegal access to memory => segfault!!
Null pointer:
NULL 0
The storage space with address 0 does not exist in the computer
If the value of a pointer is a pointer to null (NULL), it is called a null pointer.
int *p=NULL; *p=1024;//Operate the empty pointerint b=*p;//Operate the empty pointer
When we use a null pointer, it will definitely cause illegal memory access => Segment error
Arrays and pointers
Array elements are the same as ordinary variables, and array elements also have their own addresses.
Array elements also have lvalues and rvalues, and the addresses between array elements are adjacent.
The array name can represent the address of the first element.
a => &a[0], the array name a is regarded as a pointer
like
int a[]={1,2,3}; int *p=a;//a is the array name as a pointer, ==&a[0]
Problems of adding and subtracting pointers:
Can we access a[1] through pointer p?
p=p+1;//&a[0]+1==&a[1]
p + i (p is a pointer, i is an integer value)
Note: Instead of simply adding and subtracting the values, add and subtracting the length of the i pointer to the unit.
p + 1 => Move the length of an int unit backwards, &a[1]
int a[10]; int * p = a; // p = &a[0] a[1] <=> *&a[1] <=> *(&a[0] + 1) <=> *(a + 1) <=> a[1] a[1] <=> *&a[1] <=> *(&a[0] + 1) <=> *(p + 1) <=> p[1]
The array name a has two meanings in the code:
int a[10];
(1) The array name represents the entire array
&a: Take the address of the entire array a.
&a + 1: Move 1 (int[10]) unit length backwards.
(2) In the case of appropriate circumstances, the array name can be regarded as a pointer
a <=> &a[0]
a + 1 : See as a pointer
=> &a[0] + 1
=> &a[1]
Multidimensional arrays and pointers
In C language, all arrays are actually one-dimensional arrays!
int a[3][4]; //int[4] a[3];
Expression
a
(1) Treat as a pointer &a[0] �
(2) Seen as the entire array
a[0] Array name:
(1) Treat as a pointer &a[0][0] �
(2) See as the entire one-dimensional array a[0]
a[0][0] Array element a[0][0] a[0][0] (lvalue/rvalue)
a + 1 a is the array name, and it is regarded as a pointer here ==&a[0]+1==&a[1]
a[0] + 1 a[0] is the array name, which is used as a pointer here ==&a[0][0]+1==&a[0][1]
a[0][0] + 1 a[0][0] is an int element in the two-dimensional array a ==a[0][0]+1
a[1] + 2 a[1] is the array name, which is regarded as a pointer here ==&a[1][0]+2==&a[1][2]
*(a + 1) + 2 a is the array name, which is used as a pointer here ==*(&a[0]+1)+2==*(&a[1])+2==a[1]+2==&a[1][2]
Pointer Constant and Constant Pointer
They are all pointers, but there is a little difference in their attributes.
Pointer constants:
The pointer itself cannot be changed (the pointer's pointer cannot be changed), but
The contents in the memory space it points to can be changed.
The most representative example is array name!
Definition method:
Type to object * const pointer variable name;
int a , b; int * const p = &a; p = &b ; //error
Constant pointer:
is an address pointing to a constant. The object pointed to by the pointer is a constant.
Then the pointer itself can be changed, but this pointer pointer
The contents in the memory space cannot be changed.
The most representative example is string
char * p = "abcde"; //"abcde" <=> &'a'
Definition method:
const type * variable name;
or
Type const * variable name;
Pointer array and array pointer
(1) Pointer array
A pointer array is an array, but every element in it is a pointer!
Define an array:
Array element type Array name [number of elements];
"Array Element Type": Any legal type in C language is OK.
int * p[4]; //Define an array of pointers,The array name isp,It contains4indivualint*Type pointer。
(2) Array pointer
An array pointer is a pointer, but this pointer is used to point to an array!!
int (*p)[4] ; //Define an array pointer,The pointer variable name isp,Used to point to a containing4indivualintArray of type elements。
like
int a[3][4]; int (*p)[4]; p = a; //&a[0] *(*p + 1) // *(a[0]+1)==*(&a[0][1])==a[0][1] *(*(p + 2)) // *(*(&a[2]))==*(&a[2][0])==a[2][0] *(*(p+3) + 3)// *(*(&a[0]+3)+3)==*(*(&a[3])+3)==*(&a[3][0]+3)==a[3][3]
String and pointer
A string is a string of characters. In C language, there is no string type.
C language strings are implemented through char * (character pointer).
A string in C language is represented by a string of characters caused by "", and a string ending flag will be added by default after the string \0,\0 (the character with ASCII is 0).
like
"abc" //String
"\n" //String
"" //String => Empty String
'\123' //Character
'\n' //character
Note:
We only need to save the first character address of the string. Start by finding the first \0 from the first character address. The previous characters are the characters in the string.
In C language, strings (such as "sssssabcd") are stored in a memory area called .rodata (read-only data), and the string represents the first address of this memory space.
"ssssssabcd" => Value of expression => &'s' char *p="12345" ==&'1' int *p=&'1'; p+1=&'2';
Character array:
char s[5] = {'a', 'b', 'c', 'd', 'e'}; //Character array, the same as normal array, is saved in a .data/stack space //The array area is readable and writable sizeof(s) == 5 strlen(s) >= 5 //No '\0' length is uncertain
char s[] = {"abcde"}; //Automatically generated '\0' <=> char s[] = {'a', 'b', 'c', 'd', 'e', '\0'}; //S character array, the same as normal array, is saved in a .data/stack space //The array area is readable and writable sizeof(s) == 6 strlen(s) == 5
char s[] = {"abcde"}; s[1] = 'B'; //OK char ch = s[1]; //OK *(s + 1) = 'B'; //OK ch = *(s + 1); //OK s = s + 1; //ERROR //s is the array name, the array name is regarded as a pointer, pointer constant //The pointing of s cannot be changed, but the memory space pointed to by s is not changed, //The content inside is variable printf("%s\n", s); //abcde %s : char* Address //Take the address behind as the first address of a string, and output characters one by one, and it will not end until it encounters \0!
Function pointer
Functions also have addresses, so we can define a pointer to store the address of a function. A pointer like this is called a function pointer.
How to define function pointers
int * abc(int a, float b) { }
Describe the type of function abc: int (int, float): is a function type with a return value of int type and a parameter with an Int type and float type
The type of return value pointing to the function (* pointer variable name) (pointing to the function's formal parameter type list);
int (* p)(int, int) ;
Define a pointer variable p.p is a function pointer, which is used to point to a function with a return value of int type and two int type parameters.
How to assign a function pointer
&Function Name
or
Function name: In C language, the function name itself can represent the address of the function
There are two solutions to call the pointer function through the function pointer:
p is a pointer to the function.
(1) (*p)(Real parameter list)
(2) p(real argument list)
int *p=abc//Function namep(a,b);//Calling the function
Secondary pointer and multi-level pointer
int a,b;
You can define a pointer variable p1 to save the address of variable a:
int *p1=&a; b=*p1=*&a=a;//Access the a variable through pointer p1
You can define a pointer variable p2 to save the address of variable p1:
int *p2=&p1 int b=**p2=*p1=a//Accessing the a variable through pointer p2
p2 saves the address of a first-level pointer p1, so
p2 points to a first-level pointer, p2 is a second-level pointer
You can define a pointer variable p3 to save the address of variable p2:
int *p3=&p2; int b=***p3=**p2=*p1=a;
p3 saves the address of a secondary pointer p2, so
p3 points to a secondary pointer, p3 is a third-level pointer
This is the end of this article about the purpose of C language analysis pointer. For more related C language pointer content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!