SoFunction
Updated on 2025-03-03

Detailed explanation of the initial pointer in C language

1. What is a pointer

​ Beginners have a question, that is, what is the pointer? Simply put, it can be found through it with the addressMemory unit.

The address points to a certain memory space, so the address image is called a pointer.

int main()
{
	int a = 10;
	int* pa = &a;
    return 0;
}
//Pa is used to store address (pointer), so pa is a pointer variable.

Summary: Pointers are variables, variables used to store addresses. (The values ​​stored in the pointer are processed as addresses).

​ The address uniquely identifies a piece of space.

The size of the pointer is 4 bytes on the 32-bit platform and 8 bytes on the 64-bit platform.

2. Pointer and pointer types

​ We know that variables have different types (integral, floating point, character, etc.), and in fact, pointers also have different types.

​ The meaning of pointer type 1:
The pointer type determines how many bytes are accessed at a time when the pointer dereference operation is used (the size of the memory is accessed)
char* pointer dereference access 1 byte
int* pointer dereference access to four bytes

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

​ The meaning of pointer type 2:

The pointer type determines the step size when the pointer ± integer (skip a few bytes when the pointer ± integer)

int* pointer +1 Skip four bytes

char* pointer +1 skip one byte

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

3. Wild pointer

​ A wild pointer means that the position of the pointer pointing is agnostic (random, incorrect, and without explicit restrictions).

3.1 Causes of wild pointers

1. Pointer not initialized

int main()
{
    int* p;//Local variable pointer is not initialized, default is random value    *p = 20;// Find a space by using the random value stored in p as the address, which does not belong to our current program    //It causes illegal access, p is a wild pointer    return 0;
}

2. Pointer outbound access

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

3. Release of space pointed to by the pointer

int* test()
{
    int a = 10;
    return &a;
}
int main()
{
    int* p = test();
    printf("%d\n",*p);
    return 0;
}

3.2 How to avoid wild pointers

1. Pointer initialization

2. Be careful that pointers cross the boundary

3. The pointer points to the space and releases it even if NULL is set

4. Avoid returning the address of local variables

5. Check the validity before using the pointer

int main()
{
    int a = 10;
    int* p = &a;//Initialize clearly and determine the point    int* p2 = NULL;//You can initialize to NULL if you don't know where a pointer should currently point to    return 0;
}

4. Pointer operation

4.1 Pointer±integral

#define N_VALUES 5
float values[N_VALUES];
float* vp;
for(vp = &values[0]; vp < &values[N_VALUES];)
{
    *vp++ = 0;
}
int main()
{
    int arr[10] = {1,2,3,4,5,6,7,8,9,0};
    int* p = &arr[9];
    printf("%p\n",p);
    printf("%p\n",p-2);
    return 0;
}

4.2 Pointer-Pointer

​ Pointer-Pointer The absolute value of the number obtained is the number of elements between the pointer and the pointer

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

Pointer-Pointer is the premise that two pointers point to the same area

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

Application Find string length

int my_strlen(char* s)
{
	int count = 0;
	char* start = s;
	while(*s!='\0')
	{
		s++;
	}
	return s - start;
}
int main()
{
	char arr[] = "abcdef";
	int len = my_strlen(arr);
	printf("%d\n", len);
	return 0;
}

4.3 Relational operation of pointers

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

The above program can also be written like this

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

In fact, the task can be completed smoothly on most compilers, but we should still avoid writing this because the standards do not guarantee that it is feasible.

Standard Regulations

​ Allows comparison to pointers to the memory position after the last element of the array, but is not allowed to compare with pointers to the memory position before the first element.

5. Pointers and arrays

Array- It is a continuous space, which contains elements of the same type

The array size is related to the element type and the number of elements.

pointer(Variable) - is a variable, put the address

The size of the pointer variable is 4(32bit)/8(64bit) bytes

The array name is indeed the first element address

But there are twoexception:

(Array name) - The array name here is not the address of the first element, but represents the entire array. Here we calculate the size of the entire array, units or bytes.

2.&Array Name - The array name here is not the address of the first element, but represents the entire array. What you get is the address of the entire array

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

6. Secondary pointer

​ We all know that a pointer variable is a variable, and if it is a variable, it has an address. So where is the address of the pointer variable stored?

This is the secondary pointer we need to understand.

int main()
{
    int a = 10;
    int* p = &amp;a;
    int** pp = &amp;p;//PP is a secondary pointer    **pp = 20;
    printf("%d\n", a);//a = 20
    return 0;
}

7. Pointer array

​From the name, do you think a pointer array is a pointer or an array?

The answer isAn array is an array that stores pointers.

Integer array - The array that stores integers is an integer array

Character array - The array that stores characters is a character array

Pointer array - The array that stores pointers is a pointer array

int* array of integer pointers

char* array of character pointers

int main()
{
	int arr[10];
	char ch[5];
	int* parr[5];
	char* pc[6];
	return 0;
}
int main()
{
	int a = 10;
	int b = 20;
	int c = 30;
	int* parr[3] = { &a,&b,&c };
	for (int i = 0;i < 3;i++)
	{
		printf("%d\n", *(parr[i]));
	}
	return 0;
}

​ Summary

The above is all about our initial C language pointer. I will update the advanced version of C language pointer in the future. I hope everyone can have a deeper understanding of C language pointer.

That’s all for this article. I hope it can help you, and I hope you can pay more attention to more of my content!