Memory address and pointer of variable
Pointer variable
Essence: the pointer is the storage address of the defined data variable; the address is the memory address to access the variable;
Pointer variable is a memory variable used to store the address of data variable. After the assignment operation of address taking operation, the pointer variable will point to the data variable of the address taken out;
Pointer variable is generally called: pointer;
Pointer content: refers to the memory address of various defined data type operation objects;
//Compare direct and indirect access to data variables
//Compare direct and indirect access to data variables
#include "stdio.h" void main(){ int i=20; int m,n; int* i_pointer =&i; m=i+6; //Direct access to variable i data value n=*i_pointer+3; // Indirect access i, via pointer i_pointer=&m; // Pointer to m variable printf("m=%d \nn=%d \n",*i_pointer,n); // m is through indirect access, n is through direct access }
Pointer is the variable that stores the following memory address; the variable name is equivalent to the reference (take the nickname small name);
Assignment of pointer variable
Assignment of pointer variable: only address value of defined variable can be assigned, and no other data can be assigned;
C language provides the address operator "&" to represent and perform the operation of variable address;
The general form of variable address operation is: variable name; eg: number is: the operation result is the address of variable number;
Note: symbols such as "*" and "&" have polysemy, that is, they have different grammatical meanings in different environments
Pointer variable definition initialization operation:
int number;
int* p=&number;
Equivalent to:
int number;
int* p;
p=&number;
Pointer variable operators and operations
Address variable operator: '&': take address operator; '*': variable value access operator;
Possible form: & * pointer operation, equivalent to & (pointer): the result is the memory address corresponding to the pointer;
(& x) operation - equivalent to: the result is the corresponding x variable value;
(pointer) + +, equivalent to x + +;
Have pointer + +, equivalent to (pointer + +);
//Simple examples
//Pointer variable operators and operations
#include "stdio.h" void main(){ int a=10,b=20; int* pointer =&a; // pointer points to a a=*pointer+5; pointer=&b; //pointer points to b; b=*pointer +a; printf("a is %d,b is %d \n",a,*pointer); printf("b address is %d \n",pointer); }
Pointer variable operation
Eg1:
int x;
int* px=&x;
int* pb;
pb=px;
Eg2:
int x[5],*px;
px=x; array name is the first address;
Eg3:
char *pc;
pc = "c language" / / that is to assign the first address of the string to pc;
2. Addition and subtraction operator
The addition and subtraction operators of pointers have no meaning for a single variable, mainly for pointer variables pointing to arrays or linked lists; the operation of addition and subtraction operators is based on the length of data type;
int x[5];
int* px;
PX = x; / / PX points to the first address of array x, that is, the address of array element x[0]
px = px + 2; / / px points to the address of x[2], that is, the address whose px value is & x[2]
3. Null pointer operation
Null pointer p=NULL means that pointer variable p does not point to any data variable;
if(p=NULL) ...
Note: NULL pointer and pointer unassigned are different; NULL pointer p=NULL, which is equivalent to the pointer variable that is regarded as NULL and does not point to any variable, but the unassigned pointer, which has no point and the address value is uncertain, may point randomly and damage the system; therefore, once the pointer is defined, it must be initialized by assignment to make it point to some extent
Pointer variable as function parameter
As a function parameter, pointer variable transfers the address of variable to another function parameter, which is the actual operation of variable data. Because it transfers the address of data variable, all the data values of actual variable can be operated, but the actual pointer parameter itself has no relationship with the formal pointer parameter, which is still a one-way transmission;
//Write a program, input two numbers arbitrarily, and exchange two variable values through function call;
//Pointer variables are passed as function parameters to operate the actual variables
#include "stdio.h" #include "string.h" void swop(int* pa,int* pb); void main(){ int a,b; int* pa=&a; int* pb=&b; char str[]="Please enter two numbers:"; puts(str); scanf("%d %d",&a,&b); printf("before the swop is %d,%d",a,b); putchar('\n'); // Exchange two functions swop(pa,pb); printf("after the swop is %d,%d \n",a,b); } void swop(int* pa,int* pb){ int temp; temp=*pa; *pa=*pb; *pb=temp; }
Exchange process of argument value:
Change from data variable to pointer variable
//Change the local variable temp in the Swap() function in the above program from data variable to pointer variable; difference
void swop(int* pa,int* pb){ int* temp; temp=pa; pa=pb; pb=temp; } // As a result, these two numbers have not changed, because this is only the pointer address of the formal parameter exchanged, as shown in the figure below;
Good questions?
1. When the program is running, how is the computer memory area divided?
2. When accessing a memory variable, how is the memory variable address and the data in the memory variable storage unit under the address operated and implemented in terms of storage access?
3. Discuss how direct access and indirect access to memory variable data differ?
4. Discuss how to get the address of the defined variable and how to store the variable data according to the address?
5. What are the actual and effective address operation operators in pointer operation, and what are the physical characteristics of each operation?
6. When a function parameter is of pointer type, what is passed to the specified variable?
Change from pointer variable to data variable
//Pointer variables are passed as function parameters to operate the actual variables
#include "stdio.h" #include "string.h" void swop(int x,int y); void main(){ int a,b; int* pa=&a; int* pb=&b; char str[]="Please enter two numbers:"; puts(str); scanf("%d %d",&a,&b); printf("before the swop is %d,%d",a,b); putchar('\n'); // Exchange two functions swop(a,b); printf("after the swop is %d,%d \n",a,b); } void swop(int x,int y){ int temp; temp=x; x=y; y=temp; }
//And it didn't change
Parameter value exchange process: one way transfer process
When the data variable name is defined, it will always be bound to the allocated memory address instead of being equal. It represents the data variable value on the memory. The value can be changed arbitrarily, but the variable name and memory address remain unchanged;
**Data variable name represents: container; memory address represents: factory number location; pointer variable represents: record book record number
*Int x = 100; int p = & X; X + +; (* P) + +; P + + / / result: 102, then the pointer P does not point to X;
Array and address pointer
Array is composed of array elements stored in continuous memory; array name is the first address of the array, that is, array[0];
Array element reference method:
1. The following table method: array [2] 2, pointer method: * (p+2);
//Write the program, using the following table method and pointer method
Pointer method:
Array and address pointer are two ways to express array
// Array subscript method /*#include "stdio.h" void main(){ // Define variable area int a[6]; int i=0; printf("Please enter 6 numbers: "; for(i=0;i<6;i++){ scanf("%d",&a[i]); } printf("\n"); // Output array printf("Array output: '); for(i=0;i<6;i++){ // The semicolon in the parenthesis is really hard to find printf("%3d",a[i]); } printf("\n"); } */ // Using the pointer method #include "stdio.h" void main(){ // Define variable area int a[6]; int i; int* p=a; //Input data area printf("Please enter 6 numbers:"); for(p=a;p<a+6;p++){ scanf("%d",p); // The pointer variable itself is an address, so you don't need to get its address again } // Output array printf("The array is:"); p=a; for(i=0;i<6;i++){ printf("%3d",*p); p++; } printf("\n"); }
Array name function parameter
// Store array elements in array x in reverse order // Name the array as a function parameter and store the elements in the array in reverse order #include "stdio.h" #define N 5 int swop(int y[]); void main(){ // Define variable area int a[N]; // Defining an array of arguments int i; //Define arrays 1-5; output for(i=0;i<N;i++){ a[i]=i+1; } printf("the array is:"); for(i=0;i<N;i++){ printf(" %-3d",a[i]); } printf("\n"); // Array storage in reverse order swop(a); // output printf("after the swop:\n"); printf("the array is:"); for(i=0;i<N;i++){ printf("%-3d",a[i]); } printf("\n"); } int swop(int y[]){ // Or define a pointer variable here int i=0; int* p=y; int temp; for(i=0;i<N/2;i++){ temp=*(p+i); *(p+i)=*(p+N-1-i); *(p+N-1-i)=temp; } return 0; }
Pointer to array as function parameter
The pointer variable can point to the address, and the pointer variable, as the parameter of the function, points to the array to realize the transmission of address data
How array and pointer variables pass array addresses:
//Both arguments use pointer variables
// Both arguments use pointer variables #include "stdio.h" #define N 5 int doubled(int* p); // Declaring function void main(){ int x[N]; // Defined array int i =0; // Circular pointer printf("Enter this array:"); // for(i=0;i<N;i++){ printf("x[%d]=",i); scanf("%d",x+i); // x is the first address } printf("the array is : \n"); for(i=0;i<N;i++){ printf("x[%d]= %-3d ",i,*(x+i)); // Numerical value } printf("after doubled is:\n"); doubled(x); // Output array for(i=0;i<N;i++){ printf("x[%d]=%-3d",i,*(x+i)); } printf("\n"); } int doubled(int* p){ int i; for(i=0;i<N;i++){ *(p+i)=*(p+i)*2; } return 0; }
;
Multidimensional array and pointer variable
Correspondence between 2D array and pointer variable:
Pointer of pointer: secondary pointer;
In practice: mainly used are: first level pointer and second level pointer
int a =100;
int p1 = &a;
int p2 = &p1;
Pointer variable is also a kind of variable, which also takes up storage space. You can also use & to get its address. C language does not limit the number of pointers. Every time you add a pointer, you have to add an asterisk when defining pointer variables. p1 is the first level pointer, pointing to data of common type, and there is one when defining; p2 is the second level pointer, pointing to the first level pointer p1, and there are two when defining.
Traversing 2D array with pointer
#include <stdio.h> int main(){ int a[3][4]={0,1,2,3,4,5,6,7,8,9,10,11}; int(*p)[4]; // It means that p+1 jumps 2 * 4 bytes directly to the first address of the next line int i,j; p=a; for(i=0; i<3; i++){ for(j=0; j<4; j++) printf("%2d ",*(*(p+i)+j)); printf("\n"); } return 0; }
Pointer variable points to row output two-dimensional matrix
//Use pointer variable to output 2D array
#include "stdio.h" void main(){ // Define variable area int a[2][3]= {{1,2,3},{4,5,6}}; int (*p)[3]; // This is to define a pointer variable to a one-dimensional array, so the definition is basically a two-dimensional array; int* p[3]: This is to define a one-dimensional pointer array, and the array element is a pointer int i; int j; p=a; // Direction line for(i=0;i<2;i++){ for(j=0;j<3;j++){ printf("%-3d",*(*(p+i)+j)); // This is also a first level pointer, * (p+i) is actually a line, only the first address of the line in this place } printf("\n"); } printf("a[%d][%d]=%d \n",i-1,j-1,*(*(p+1)+2)); }
Understand the addressing method of multidimensional array and the characteristics of accessing array elements.
Address is used to process two-dimensional array operation, and how to access and process array element data.
Basic way to access 2D array with pointer
// Use pointer variable to output 2D array /*#include "stdio.h" void main(){ // Define variable area int a[2][3]= {{1,2,3},{4,5,6}}; int (*p)[3]; // This is to define a pointer variable to a one-dimensional array, so the definition is basically a two-dimensional array; int* p[3]: This is to define a one-dimensional pointer array, and the array element is a pointer int i; int j; p=a; // Direction line for(i=0;i<2;i++){ for(j=0;j<3;j++){ printf("%-3d",*(*(p+i)+j)); // This is also a first level pointer, * (p+i) is actually a line, only the first address of the line in this place } printf("\n"); } printf("a[%d][%d]=%d \n",i-1,j-1,*(*(p+1)+2)); }*/ // Using another pointer variable to manipulate a 2D array #include "stdio.h" void main(){ // Define variable area int a[2][3]={{1,2,3},{4,5,6}}; int i; int* p; p=&a[0][0];// Or p=a[0], or p=*a, pointing to the first address of the two-dimensional array for(i=0;i<6;i++){ if((p-a[0]) %3==0 && p!=a[0]){ printf("\n"); } printf("%-5d",*p++); } printf("\n"); }
In a two-dimensional array, if you need to calculate the position of the specified array element, you need to calculate the offset of the element in the array relative to the first address of the array, that is, the relative position;
If array array[m][n] is set, the formula for calculating the relative position of element array[i][j] in array is as follows:
i*m+j; that is (p+im+j);