Basic concepts of C language pointer

1, What is the pointer?

In computer science, pointer is an object in programming language.
You can use the address to find a value that points to another place in the computer memory. Since the variable unit can be found through the address, it can be simply said that the address points to the variable unit.
Therefore, the address visualization is called a pointer. You can find the memory unit with its address through the pointer.

  • We can understand this:
    Memory: data stored in the hard disk.
    Pointer: is an address where the data can be found, which is stored by the variable.

  • Corresponding to code:
    First define a variable a, and the system will open up a space in memory;
    Then use the & operator to take out the address of a;
    Finally, the pointer variable p is defined to store the address of a.

#include <stdio.h>
int main() {
	int a = 10;
	int* p = &a;
	return 0;
}
  • Summary: pointer is a variable used to store address variables. (the value stored in the pointer is treated as an address)

2, Pointer and pointer type

  1. We all know that variables have different types, integer, floating point, etc. Does the pointer have a type? What types are there?

When there is such a code:

	int num = 10;
	p = &num;

To save & num to p, we know that p is a pointer variable. What should its type be?

	char *pc = NULL;
	int *pi = NULL;
	short *ps = NULL;
	long *pl = NULL;
	float *pf = NULL;
	double *pd = NULL;
	
Pointers are defined by type + * + Pointer name.

Actually, char* Type pointer is used to store char Address of the type variable.
short* Type pointer is used to store short Address of the type variable.
int* Type pointer is used to store int Address of the type variable.
  1. What is the meaning of pointer type? Can the addresses of type variables be stored with different types of pointers?
    Let's look at the following code:
#include <stdio.h>
int main() {
	int n = 10;
	char* pc = (char*)&n;
	int* pi = &n;

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

	return 0;
}


Summary: looking at the result output, we can find that the type of pointer determines how far the pointer moves forward or backward.

  1. Dereference of pointer
#include <stdio.h>
int main() {
	int n = 0x11223344;

	char* pc = (char*)&n;
	int* pi = &n;
	//Focus on the memory changes during debugging
	*pc = 0;
	*pi = 0;

	return 0;
}

Summary: the type of pointer determines how much permission you have when dereferencing the pointer (can operate several bytes). For example, the pointer dereference of char can only access one byte, and the pointer dereference of int can access four bytes.

3, Field pointer

Concept: the wild pointer is that the position pointed to by the pointer is unknown (random, incorrect and without explicit restrictions).

Origin of wild pointer

  1. Pointer not initialized
#include <stdio.h>
int main() {
	int* p; //Pointer variable is uninitialized and defaults to random value
	*p = 20;

	return 0;
}
  1. Pointer out of bounds access
#include <stdio.h>
int main() {
	int arr[10] = { 0 };
	int* p = arr;
	int i = 0;
	for (i = 0; i <= 11; i++) {
		//When the pointer points beyond the range of arr, p is the wild pointer
		*(p++) = i;
	}

	return 0;
}
  1. The space pointed to by the pointer is released
    Later, I learned to dynamically open up memory, and the pointer points to the memory opened up by myself. Forget to reasonably end the pointer after the operation, resulting in the pointer still pointing to the previous space and becoming a wild pointer.

How to avoid wild pointers

  1. Pointer initialization
  2. Be careful that the pointer is out of range
  3. When the pointer points to space release, the pointer is set to NULL
  4. Check the validity of the pointer before use
#include <stdio.h>
int main() {
	int* p = NULL;
	int a = 10;

	p = &a;

	if (p != NULL) {
		*p = 20;
	}
	return 0;
}

4, Pointer operation

  • Pointer integer operation
  • Pointer & pointer operation
  • Relational operation of pointer

Pointer integer operation

#define N_VALUES 5;
float *vp;
float values[N_VALUES];
for(vp=&values[0];vp<&values[N_VALUES];){
	*vp++ = 0;
}

Pointer & pointer operation

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

Relational operation of pointer

for(vp=&values[N_VALUES]; vp>&values[0];){
	*--p = 0;
}

This code may violate the standard.

Standard provisions:
A pointer to an array element is allowed to be compared with a pointer to the memory location after the last element of the pointer array, but it is not allowed to be compared with a pointer to the memory location before the first element.

5, Pointers and arrays

What is the array name? Let's take an example:

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


From the operation results, we know that:
The array name and the address of the first element of the number are the same.

So we can conclude that the array name represents the address of the first element of the array.

Since the array name can be stored as an address in a pointer, we can use the pointer to access the array.
For example:

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

	return 0;
}

Operation results:

p+i actually calculates the address of the array subscript i.
Then we can directly use the pointer to access the array.
As follows:

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

	return 0;
}

6, Secondary pointer

Pointer variable is also a variable. If a variable has an address in memory, where is the address of pointer variable stored? This is the secondary pointer (dolls).

The operations of the secondary pointer are:

1. yes ppa Dereference the address of and find pa. 
int b = 20;
*ppa = &b; 

amount to pa = &b; One * Just one layer ppa
2. **ppa First pass *ppa find pa,Then yes pa To dereference:*pa,Then find it a. 
**ppa = 30;

amount to *pa = 30;
Two ** Just solve two layers, and the pointer points to a Yes.

7, Pointer array

Is a pointer array a pointer or an array?

Answer: array. Is an array of pointers.

Array we know that there are integer arrays and character arrays

	int arr1[5];
	char arr2[6];

So what about pointer arrays?

	int* arr3[5];
arr3 Is an array with five elements, each of which is an integer pointer.

Keywords: C Back-end

Added by oneski on Thu, 13 Jan 2022 11:56:11 +0200