C language pointers (primary)

Pointers in C are interesting. With pointers, the execution of some C programming tasks can be simplified, and some tasks, such as dynamic memory allocation, cannot be performed without pointers. So, if you want to be a good C programmer, it is necessary to learn pointers. Let's get straight to the point

content

1. What is a pointer
2. Pointer and pointer type
3. Wild pointer
4. Pointer and array
5. Pointer array
6. Remember to like and go

1. What is a pointer?

In C language, variables are stored in memory, and memory is actually an array of ordered bytes, and each byte has a unique memory address. The CPU uses memory addressing to locate the address of a specified data object stored in memory. Here, the data object refers to a value or string of a specified data type stored in memory, they all have their own address, and the pointer is the variable that holds this address. That is: a pointer is a type of variable that holds the address of a variable.
It can be simply understood as:

  1. A pointer is the number of a smallest unit in memory (that is, a byte), which is an address
  2. The pointer that is usually spoken in colloquial language usually refers to a pointer variable, which is a
    summary of variables used to store memory addresses : a pointer is an address, and a pointer in colloquial language usually refers to a pointer variable.
    We can look at the memory distribution of the following code.
#include<stdio.h>
int main()
{
    
    
	int a[10] = {
    
     0 };
	int i = 0;
	for (i = 0; i < 10;i++)
	printf("%p\n", &a[i]);
	return 0;
}

The output is
insert image description here

Because the array is an integer, the difference between every two elements is 4 bytes
insert image description here

2. Pointers and pointer types

example code

int num = 10;
 p = &num;

To save &num (the address of num) into p, we know that p is a pointer variable, so what is its type?
We give the pointer variable the corresponding type.

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

As you can see here, pointers are defined as: type + * .
In fact:
the char* type pointer is to store the address of the char type variable.
A pointer of type short* is used to store the address of a variable of type short.
A pointer of type int* is used to store the address of a variable of type int.

see the following code

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; }

insert image description here
Summary: The type of the pointer determines how far (distance) the pointer goes forward or backward.
For example, int takes 4 bytes in one step, char takes 1 byte in one step...
pointer dereference

#include <stdio.h>
int main()
{
    
    
 int n = 0x11223344;
 char *pc = (char *)&n;
 int *pi = &n;
 *pc = 0;   //重点在调试的过程中观察内存的变化。
 *pi = 0;   //重点在调试的过程中观察内存的变化。
 return 0; }

Summary:
The type of pointer determines how much authority it has when dereferencing the pointer (how many bytes can be manipulated).
For example: a char* pointer dereference can only access one byte, and an int* pointer dereference can access four bytes.

3. Wild pointer

Concept: A wild pointer means that the position pointed to by the pointer is unknown (random, incorrect, and not clearly limited)
(1) Causes of wild pointer
a. Pointer is not initialized

#include <stdio.h>
int main()
{
    
     
 int *p;//局部变量指针未初始化,默认为随机值
    *p = 20;
 return 0; }

b. 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++)
   {
    
    
        //当指针指向的范围超出数组arr的范围时,p就是野指针
        *(p++) = i;
   }
    return 0; 
}

c. How to release the space pointed to by the pointer
(2) how to avoid wild pointers

  1. pointer initialization
  2. Watch out for pointer out of bounds
  3. The pointer to the space is released even if it is set to NULL
  4. Avoid returning the address of a local variable
  5. Check validity of pointer before use

4. Pointers and arrays

code above

#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; }

insert image description here
It can be seen that the array name and the address of the first element of the array are the same.
**Conclusion:** The array name represents the address of the first element of the array. ( except **sizeof(array)** and &array )

Then we can also access the array directly through the pointer.

int main()
{
    
    
 int arr[] = {
    
     1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
 int *p = arr; //指针存放数组首元素的地址
 int sz = sizeof(arr) / sizeof(arr[0]);
 int i = 0;
 for (i = 0; i<sz; i++)
 {
    
    
 printf("%d ", *(p + i));
 }
 return 0; }

insert image description here

5. Array of pointers

An array of pointers, as the name suggests, is an array, an array used to store pointers.
We already know that integer arrays, character arrays

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

insert image description here
What about an array of pointers?

int* arr3[5];//是什么?

insert image description here
arr3 is an array with five elements, each element is an integer pointer.
insert image description here

Guess you like

Origin blog.csdn.net/qq_62316056/article/details/124285163