C language -- first knowledge of pointer

Keywords: C Back-end

What is the pointer

In computer science, a pointer is an object in a programming language. Using an address (memory number), its value directly points to a value stored in another place in computer memory.
Since the required variable unit can be found through the address, it can be understood that the address points to the change unit.
Therefore, the address visualization is called "pointer", which means that the memory unit with its bit address can be found through it.

  1. The pointer is a variable that stores the address (number) of the memory unit, and the values in the pointer are treated as addresses;
  2. An address uniquely identifies an address space.
void main()
{
	int a = 10; //Open up a space in memory. The name of the space is a and the stored value is 10
	int* p = &a; //Open up a space in memory. The name of the space is p, and the stored value is the address of a, pointing to space a
}

Pointer size

  1. On a 32-bit machine, the address is a binary sequence composed of 32 zeros and 1, so the address has to be stored in 4 bytes, so the size of a pointer variable is 4 bytes.
  2. On a 64 bit machine, the address is composed of a 64 bit binary sequence, which requires 8 bytes of space to store the address, so the size of a pointer variable is 8 bytes.

Pointer and pointer type

Type of pointer

char *pc = NULL;
int *pi = NULL;
short *ps = NULL;
long *pl = NULL;
float *pf = NULL;
double *pd = NULL;

The way to define a pointer is: type * pointer name.
Where type refers to the address of what type the pointer stores; For example, a pointer of type char * is used to store the address of a variable of type char.

Meaning of pointer type:

The type of pointer determines how far the pointer moves forward or backward.

Code example:

void main()
{
	int n = 10;
	char* pc = &n;
	int* pi = &n;

	printf("%p\n", &n);
	printf("------------\n");
	printf("%p\n", pc);
	printf("%p\n", pc+1);
	printf("------------\n");
	printf("%p\n", pi);
	printf("%p\n", pi+1);

}

As can be seen from the above code, the type of pointer determines the distance the pointer takes one step forward or backward.

Dereference of pointer

void main()
{
	int n = 0x11223344;
	int n1 = 0x11223344;
	char* pc = (char *)&n;
	int* pi = &n1;
	*pc = 0; //char * type can only access one byte.
	*pi = 0; //int * type can only access four bytes.
	printf("0x%x\n", n);
	printf("0x%x\n", n1);

}

The type of pointer determines how much permission (several bytes can be operated) you have when dereferencing the pointer.

Field pointer

The wild pointer is that the position pointed to by the pointer is unknown (random, incorrect and unrestricted).

Origin of wild pointer

  1. Pointer not initialized
  2. Pointer out of bounds access
  3. The space pointed to by the pointer is released
void main()
{
	int* p; //The local variable pointer is uninitialized and defaults to a random value;

	int arr[10] = { 0 };
	int* pi = arr;
	int i = 0;
	for (i = 0; i <= 11; i++)
	{
		//When the range pointed to by the pointer exceeds the range of array arr, pi is a wild pointer
		*(pi++) = i;
	}
}

If you avoid the field pointer

  1. Pointer initialization
  2. Be careful that the pointer is out of range
  3. The space pointed to by the pointer is set to NULL
  4. Check the validity of the pointer before use
void main()
{
	int* p = NULL;
	int a = 10;
	p = &a;
	if (p != NULL) //Effectiveness check
	{
		*p = 20;
	}
	printf("%d", a);
}

Pointer operation

void main()
{
    char arr[5]="";
	char* p = arr;
	//Use pointers to assign values to strings
	for (int i = 0; i < 4; i++)
	{
		*(p + i) = '1';
	}
	*(p + 4) = '\0';
	printf("%s", arr); //1111
}

Pointer ± integer

void main()
{
    char arr[5]="";
	char* p = arr;
	//Use pointers to assign values to strings
	for (int i = 0; i < 4; i++)
	{
		*(p + i) = '1';
	}
	*(p + 4) = '\0';
	printf("%s", arr);
}

Pointer pointer

//Use pointer pointer to complete the function of finding the length of string
int my_strlen(char* s)
{
	char* p = s;
	while (*p != '\0')
	{
		p++;
	}
	return p - s;
}
void main()
{
	char arr[] = "123456";
	int n = my_strlen(arr);
	printf("%d\n", n);
	printf("%d\n", strlen(arr));
}

Relational operation of pointer

Standard provisions:

Allows the pointer of the execution array element to be compared with the pointer of the memory location after the last element of the execution array. However, it is not allowed to compare with a pointer to the memory location before the first element.

For example:
for(vp = &values[N_VALUES-1]; vp >= &values[0];vp–)
{
*vp = 0;
}
Such code may execute smoothly, but it should be avoided because the standard does not guarantee its feasibility.

Pointers and arrays

As discussed in the previous notes, the numeric name and the address of the first element of the array are the same. Therefore, the following code is feasible:

void main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,0 };
	int *p = arr; //The address of the first element of the array stored in p
}

Since you can store the array name as an address in a pointer, you can use the pointer to access the array.

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

From the running results of the above code, it can be found that, (p+i) actually calculates the address of array arr subscript i;
Therefore, you can directly use the pointer to access the array:

void main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
	int* p = arr; //The pointer stores the address of the first element of the array
	int sz = sizeof(arr) / sizeof(arr[0]);
	//Print the elements of the array with a pointer
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", *(p + i));
	}
}

Secondary pointer

The pointer variable is also a variable. If it is a variable, it has an address. Then the secondary pointer stores the address of the pointer variable

int a = 10;
int *pa = &a;
int **ppa = &pa; // Secondary pointer

**ppa first finds pa through * ppa. After dereferencing PA, it finds a.

Code example:

void main()
{
	int a = 10;
	printf("%d\n", a);

	int* pa = &a;
	int** ppa = &pa;
	**ppa = 30;
	printf("%d\n", a); //The value of a becomes 30
}

Pointer array

A pointer array is an array that stores pointers

int* arr[5];
//arr is an array of integer pointers with five elements, each of which is an integer pointer.

Posted by Mhz2020 on Sat, 20 Nov 2021 18:25:54 -0800