2021.12. 19 on the preliminary order of c language pointer

catalogue

What is the pointer

Pointer type

The types of pointers are as follows:

Meaning of pointer type:

Field pointer

Pointer operation

Pointers and arrays

Secondary pointer

What is the pointer

definition:

Pointer is an important concept and characteristic in C language, and it is also a difficult part to master C language. Pointers are memory addresses. Pointer variables are variables used to store memory addresses. The length of storage units occupied by different types of pointer variables is the same, and the length of storage space occupied by variables storing data is also different due to different types of data.

Example 1:

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

int main()
{
	int a = 10;
	int* p = &a;  //Take the address of a and use the pointer variable p to receive its address
	printf("%p\n", &a);
	printf("%p\n", p);  
	return 0;
}

The result is: (we know from the result that the pointer variable p stores the address of integer a)

Example 2:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
 
int main()
{
	int a = 10;
	printf("%p\n", &a); // &Address operator for   
	int* pa = &a;  // Here, * means that pa is a pointer variable, and pa is the address used to store a
	*pa = 20;  // *The indirect access operator (dereference operator) pa changes the value of a according to the address of A                
	printf("%d\n", a);
    return 0;
}
 

The result is:

Pointer type

The types of pointers are as follows:

What is the byte size of each type of pointer?

give an example:

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

int main()
{
	int* pa;
	short* ps;
	char* pc;
	float* pf;
	double* pd;
	printf("%d\n", sizeof(pa));
	printf("%d\n", sizeof(ps));
	printf("%d\n", sizeof(pc));
	printf("%d\n", sizeof(pf));
	printf("%d\n", sizeof(pd));
	return 0;
}

The result is: (we can get from the result that regardless of the type of pointer, its byte size is 4!)

Meaning of pointer type:

Note: Although the byte size of each type of pointer is 4, there are differences among various types of pointers.

a: The type of pointer determines the permission of} pointer dereference.

For example: (we dereference integer a with different types of pointers and see the change of its storage)

int a =0x11223344; (a in this case is a hexadecimal number)

The storage in the integer a initial address is:

(int type pointer can dereference 4 bytes)

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

int main()
{
	int a = 0x11223344; 
	int* pa = &a;
	*pa = 0;
	return 0;
}

The storage in the integer a address is changed to:

(pointer of char type can dereference 1 byte)

int main()
{
	int a = 0x11223344;
	char* pa = &a;
	*pa = 0;
	return 0;
}

The storage in the integer a address is changed to:

b: The type of pointer determines how far the pointer takes a step.

give an example:

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

int main()
{
	int arr[10] = { 0 };
	int* pa = arr;
	char* pc = arr;
	printf("%p\n", pa);
	printf("%p\n", pa+1);
	printf("%p\n", pc);
	printf("%p\n", pc+1);
	return 0;
}

The result is: (it can be concluded that the integer pointer + 1 goes 4 bytes and the character pointer + 1 goes 1 byte)

Field pointer

Definition: the wild pointer is the position pointed to by the pointer, which is unknown (random, incorrect and unrestricted) if the pointer variable is not initialized during definition, its value is random, and the value of the pointer variable is the address of other variables, which means that the pointer points to a variable whose address is uncertain. At this time, de referencing is to access an uncertain address, so the result is unknown.

give an example:

Case 1 (pointer uninitialized)

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS


int main()
{
	int* p;    
	*p = 20;  
	return 0;
}

Note: the local variable pointer is not initialized and defaults to a random value. At this time, when we dereference its pointer variable, it is illegal access. Because the accessed address does not belong to our current program, p at this time is called a wild pointer.

Case 2 (pointer out of bounds access)

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

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

The diagram is as follows:

Case 3 (the space pointed to by the pointer is released)

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

int* test()
{
	int a = 10;
	return &a;     //At this point, our variable p stores the address of a (free space)
}                  //But with the test() function, the life cycle of a ends and its spatial address is returned to the operating system

int main()
{
	int* p = test(); //At this point, we call the test() function
//After calling, we still access the address of a stored in the pointer variable p. at this time, it is illegal access and p is a wild pointer    
    *p = 20;     
	return 0;
}

Ways to avoid wild pointers:

a: The pointer needs to be initialized. If you don't know why, you can directly initialize it to NULL (NULL pointer)

b: Note that the pointer is out of bounds!

c: When the space pointed to by the pointer is released, we should even set the pointer variable to NULL

d: Check the validity of the pointer before using it

give an example:

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

int main()
{
	int* p = NULL;
	*p = 10;
	return 0;
}

Note: NULL is 0, and the address space of 0 does not belong to our user, so it cannot be dereferenced directly!

To judge the validity of the pointer, we can modify the code as follows: (first judge whether it is a null pointer)

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

int main()
{
	int* p = NULL;
	if(p!=NULL)
	*p = 10;
	return 0;
}

Pointer operation

Pointer + - integer

give an example:

(int type pointer + 1 = + 4 bytes + 2 = + 8 bytes) (char type pointer + 1 = + 1 bytes + 2 = + 2 bytes)

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

int main()
{
	int arr[5];
	int* pa;
	for (pa = &arr[0]; pa < &arr[5];)
	{
		*pa++ = 0;
	}
	return 0;
}

This code initializes the arr array to 0. At this time, * pa++=0, and then adds its corresponding byte size according to its pointer variable type, so as to complete the initialization of the array.

Application: (print each element in the array with pointer)

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

int main()
{
	int arr[10] = {1,2,3,4,5,6,7,8,9,10};
	int* pa=arr;
	int* pc = arr + 9;
	while (pa<=pc)
	{
		printf("%d ", *pa);
		pa++;
	}
	return 0;
}

The result is:

Pointer pointer

give an example:

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

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

The result is: (it can be seen that pointer pointer gets the number of elements between two pointers)

Note: two pointers should point to the same space!

Example: (incorrect use) (this is a typical case where two pointers point to different spaces)

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

int main()
{
	int arr[10] = {1,2,3,4,5,6,7,8,9,10};
	int* pa=arr;
	char brr[5];
	char* pd = brr;
	int a = pd - pa ;
	printf(" %d\n", a);
}

Application: (use pointer to find string length)

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

int my_strlen(char* start)
{
	 char * end = start;
	 while (*end != '\0')
	 {
		 end++;
	 }
	 return end-start;
}

int main() 
{
	char arr[] = { "abc" };
	char* pa = arr;
	int ret = my_strlen(pa);
	printf(" %d\n", ret);
	return 0;
}

result:

Pointers and arrays

What we need to know about arrays:

Knowledge point 1:

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

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

The result is:

 

Knowledge point 2: the array name is the address of the first element of the array

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

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

The result is:

Application: (pointer initializes the array and prints the elements of the array)

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

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

The result is:

Secondary pointer

give an example:

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS

int main()
{
	int a = 10;
	int* pa = &a;      // pa is the first level pointer of pointer variable
	int**ppa = &pa;	  //  Since PA is a variable, it will have its address, &pa takes out its address and stores it in ppa
	// At this time, paa is the secondary pointer dereference * PPA = = PA * PA = = a * * PPA = = a
	return 0;
}

Be sure to understand that "int * * PPA = & PA" Why are there two '*' here

We first observe the first level pointer int * PA = & a , where the type of integer variable a is int

Secondly, observe that the second level pointer is int * * PPA = & pa. here, the type of integer pointer variable pa is int *

There are so many contents of the primary pointer, and bloggers will further improve the contents of the advanced pointer in the later stage.

Thank you for browsing. It's not easy for bloggers to write articles. You can leave your praise or collect to inspire bloggers!

If there are mistakes in the article, please point out more, and I will correct them in time!  

Keywords: C Back-end pointer

Added by naveendk.55 on Thu, 23 Dec 2021 20:43:59 +0200