Latent C language - primary pointer

Posted by cac818 on Fri, 04 Feb 2022 02:37:43 +0100

Pointer:

Pointer is an object in the programming language. Using the address, its value directly points to the value stored in another place in the computer memory. Because the required variable unit can be found through the address, and the address points to the variable unit, the address is visualized as "pointer", which means that the memory unit with his address can be found through him. It describes the position of data in memory and indicates the relative distance value of an entity occupying storage space and the starting position of this space. In C/C + + language, pointer is generally regarded as pointer variable. The content of pointer variable stores the first address of the object it points to. The object it points to can be variable (pointer variable is also variable), array, function and other entities occupying storage space.

int main()
{
	int a = 10;
	int* p = &a;//Pointer variable
	return 0;
}

Note: the pointer is a variable used to store the address (the values stored in the pointer are treated as addresses)

Pointer and pointer type

The pointer type determines the size of the space that can be accessed when the pointer is dereferenced and the permission to dereference the pointer (several bytes can be operated)

int* p: *p can access four bytes

char* p: * p can access a byte

double* p: * p can access eight bytes

Meaning of pointer

Pointer + - integer

int main()
{
	int a = 0x11223344;
	int* pa = &a;
	char* pc = &a;
	printf("%p\n", pa);
	printf("%p\n", pa+1);

	printf("%p\n", pc);
	printf("%p\n", pc+1);
	return 0;
}

int* p: p+1 ->  4

char* p; p+1-> 1

double*p: p+1->8

sum: the pointer type determines how far the pointer goes in one step (the step size of the pointer) (distance)

Field pointer

Concept: Wild pointer means that the position pointed by the pointer is unknowable (random, incorrect and without definite limit)

1. Pointer not initialized

int main()
{
	//int a;// Local variables are not initialized and default to random values
	int* p;//Local pointer variables are initialized with random values
	return 0;
}

2. Pointer cross-border access

int main()
{
	int arr[10] = { 0 };
	int* p = arr;
	int i = 0;
	for (i = 0; i < 12; i++)
	{
		p++;
	}

	return 0;
}

When the range pointed by the pointer exceeds the range of array arr, p is the wild pointer

3. The space pointed by the pointer is released

test()
{
	int a = 10;//Local variable a
	return &a;
}
int main()
{
	int* p = test();
	*p = 20;
	return 0;
}

How to avoid wild pointers:

1. Pointer initialization

2. Be careful that the pointer is out of range

3. Set NULL when the pointer points to space release

4. Check the validity of the pointer before use

int main()
{
	int a = 10;
	int* pa = &a;//initialization
	int* p = NULL;//For initialization, assign a value to the pointer
	return 0;
}

Pointer operation

1. Pointer + - integer

2. Pointer pointer

3. Pointer relation operation

Pointer + - integer

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	int* p = arr;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", *p);
		p = p + 1;
	}
	return 0;
}

Pointer pointer

Pointer minus pointer gets the number of elements between pointer and pointer

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	printf("%d\n", &arr[9] - &arr[0]);

	return 0;
}

Use the pointer to subtract the pointer to find the length of the string

int my_strlen(char* str)
{
	char* start = str;
	char* end = str;
	while (*end != '\0')
	{

		end++;
	}
	return end - start;//Pointer minus pointer
}

int main()
{
	//strlen - find string length
	char arr[] = "bit";
	int len = my_strlen(arr);
	printf("%d\n", len);

	return 0;
}

Relational operation of pointer (compare size)

Regulation: it is allowed to compare the pointer pointing to the array with the pointer pointing to the memory location after the last element of the array, but it is not allowed to compare with the pointer pointing to the memory location before the first element

Pointers and arrays

int main()
{
	int arr[10] = { 0 };
	printf("%p\n", arr);//Address - the address of the first element
	printf("%p\n", &arr[0]);
	return 0;
}

1. & arr - & array name - the array name is not the address of the first element - the array name indicates the entire array - & the array name takes out the address of the entire array
2. Sizeof (ARR) - sizeof (array name) - the entire array represented by the array name - sizeof (array name) calculates the size of the entire array

int main()
{
	int arr[10] = { 0 };
	int* p = arr;
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%p =========  %p\n", p + i, &arr[i]);
	}
	return 0;
}

In fact, p + i calculates the address of array arr subscript i

Then we can access the array directly through the pointer

Secondary pointer

int main()
{
	int a = 10;
	int* pa = &a;
	int* * ppa = &pa;//ppa is the secondary pointer
	int** * pppa = &ppa; 

	return 0;
}

Pointer array: array: an array that holds pointers

Array pointers: pointers

Topics: C