C Language Programming Learning-Pointer

Pointer

Definition and use of pointer variables:

  1. Pointer is also a data type and pointer variable is a variable.
  2. To whom the pointer variable points, assign its address to the pointer variable
  3. The "*" operator operates on the memory space pointed by the pointer variable
#include <stdio.h>

int main()
{
	int a = 0;
	char b = 100;
	printf("%p, %p\n", &a, &b); //Print the address of a, b

	//Int * stands for a data type, int * pointer type, p is the variable name
	//Defines a pointer-type variable that can point to the address of an int-type variable
	int *p;
	p = &a;//Assign the address of a to the variable p, which is also a variable. The value is a memory address number.
	printf("%d\n", *p);//p points to the address of a, and * p is the value of A.

	char *p1 = &b;
	printf("%c\n", *p1);//* p1 points to the address of b, * p1 is the value of B.

	return 0;
}
008FF87C, 008FF873
0
d
 Press any key to continue....

Be careful

& You can get the address of a variable in memory. However, register variables cannot be retrieved because they are not in memory, but in the CPU, so there is no address.

Pointer size:

  1. When sizeof() is used to measure the size of the pointer, the result is always: 4 or 8.
  2. sizeof() measures the size of the pointer variable pointing to the storage address
  3. On a 32-bit platform, all pointers (addresses) are 32 bits (4 bytes).
  4. On 64-bit platforms, all pointers (addresses) are 64-bit (8 bytes)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main() {

	int *p1;
	char *p2;
	float *p3;
	int **p11;
	char **p22;
	float **p33;
	printf("sizeof(p1) = %d\n", sizeof(p1));
	printf("sizeof(p2) = %d\n", sizeof(p2));
	printf("sizeof(p3) = %d\n", sizeof(p3));
	printf("sizeof(p11) = %d\n", sizeof(p11));
	printf("sizeof(p22) = %d\n", sizeof(p22));
	printf("sizeof(p33) = %d\n", sizeof(p33));
}

X64

sizeof(p1) = 8
sizeof(p2) = 8
sizeof(p3) = 8
sizeof(p11) = 8
sizeof(p22) = 8
sizeof(p33) = 8
//Press any key to continue....

X86

sizeof(p1) = 4
sizeof(p2) = 4
sizeof(p3) = 4
sizeof(p11) = 4
sizeof(p22) = 4
sizeof(p33) = 4
//Press any key to continue....

Wild and null pointers:

Wild pointer: unlike defined a, it has no specific address in the program itself. It points to an unknown memory space (the area of memory that the operating system does not allow pointers to point to). Therefore, the wild pointer will not directly cause errors, and the operation of the memory area pointed by the wild pointer will cause problems.

//Wild pointer
    //Point to memory address with memory number 100
    //0-255 is reserved by the system and cannot be read or written.
    //The wild pointer (unlike a, which does not exist in the program itself) points to an unknown memory space and may or may not make mistakes when reading or writing.
	int a = 100;
	int *p;
	p = a; //Assigning the value of a to the pointer variable p, P as the wild pointer, ok, will not be a problem, but it is meaningless.
        p = 0x12345678; //Assigning a pointer variable p, P as a wild pointer, ok, will not be a problem, but it is meaningless.
        *p = 1000;  //Operating field pointer pointing to unknown area, memory problem, err

Universal pointers and pointers point to an element of the array:

Is to use void * p to create pointer variables

int a = 10;
//Universal Pointer
void *p = NULL;
p = &a;
//For universal pointer assignment, the variable type corresponding to the original value (strong rotation)
*(int *)p = 100;
printf("%d\n", a);
printf("%d\n", *(int *)p);	//You need to tell the pointer how many byte sizes to read (where int is four byte sizes)

Pointer points to an element of the array and outputs

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main() {

	int arr[10] = { 0 };	//Initialization arrays are all 0
	void* p = &arr;		//Create Universal Pointer
	*(int *)p = 100;	//Assign arr[0] to 100 (representing the initial address)
	*((int *)p + 1) = 200;	//At this point p has been converted to int type, so the + 1 operation indicates the next element location (equivalent to address + 4)
	for (int i = 0; i < 10; i++)
	{
		printf("%d\n", arr[i]);
	}
}
100
200
0
0
0
0
0
0
0
0
 Press any key to continue....

const modifies pointers

Const modifies any type of variable, and its variables cannot be changed directly. In short, const-modified variables are locked and cannot be changed.

const modifies content and results:

const modifies content

Result

const int a = 10;

//a = 100;

int *p = &a;

*p = 100;

 

The value of a constant can be modified by a first-level pointer

 const int* p;

p = &a;

p = &b;

//*p = 100;

 

const modifies pointer types

You can modify the address pointed by the pointer

int * const p = &a;

*p = 100;

//p = &b;

const modifies pointer variables

You can modify the value of the pointer to the address

const int * const p = &a;        

//p = &b;

//*p = b;

Const modifies pointer data types

Const modifies pointer variables

Neither can be modified.

 

 

The code is as follows: (See the code comments specifically)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//It's so safe that no one else can find the address.
#define MAX 100
int main401() {
	//This method is not safe and can be modified by pointer.
	//1. Modify const-modified const constants by first-level pointers
	const int a = 10;
	//a = 100;
	int *p = &a;
	*p = 100;
	printf("a Value%d\n", a);
	printf("*p Value%d\n", *p);
	return 0;
}

int main402() {

	//2. const can change the address of pointer variable, but can not directly modify the element corresponding to memory address.
	int a = 10;
	int b = 30;
	//If const modifies the pointer type, the value of the memory address pointed by the pointer variable cannot be changed, but its corresponding address can be changed.
	const int* p;
	p = &a;
	p = &b;		//Change its corresponding address
	/*	*p = 100;	Left value specifies const object	*/
	printf("%d", a);
	return 0;

}

int main403() {

	//const can change the value of the address corresponding to the pointer variable, but it cannot change the address pointed by the pointer.
	int a = 10;
	int b = 20;
	//Const modifies who, then who can't change. Here const modifies the pointer type p, P points to the address, so the address can't be changed.
	int * const p = &a;
	*p = 100;
	/*	p = &b;	Address cannot be modified*/
	printf("%d\n", *p);
	return 0;
}

int main() {

	//const modifies pointer type and pointer variable, then the address pointed by the pointer and the value pointed by the pointer
	int a = 10;
	int b = 20;
	const int * const p = &a;
	/*	All errors cannot be modified (secondary pointer can be modified)
		p = &b;
		*p = b;
	*/

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Added by Masca on Thu, 01 Aug 2019 08:11:24 +0300