Home  >  Article  >  Backend Development  >  What are the usages of pointers in C language?

What are the usages of pointers in C language?

coldplay.xixi
coldplay.xixiOriginal
2020-11-02 09:12:5517919browse

C language pointer usage: 1. The default value of pointer variables is uncertain like ordinary variables, and is generally initialized to NULL; 2. The usage of pointer variables is different from ordinary variables, and is generally distinguished by ending with p; 3. Pointers The type of the variable determines how many bytes are accessed when accessing memory through a pointer variable.

What are the usages of pointers in C language?

C language pointer usage:

1. Pointer definition:

The value range of pointer variables is 0~4G, which is a data type (unsigned integer, representing the memory number). It can be used to define variables (the same as int and long). Unlike int and long, it stores an integer that represents the memory number. Through this variable, the memory with the corresponding number can be accessed.

2. The role of pointers

1. Heap memory cannot be named (identifiers cannot be used to establish contact with heap memory), and pointers must be used.

2. Parameters between functions are transferred by value (memory copy). Using pointers can optimize the efficiency of parameter transfer (variables need to be protected). Because C language uses value transfer (memory copy), the operating efficiency will decrease as the number of variable bytes increases. The address of the transferred variable will always copy only 4 or 8 bytes.

void func(const int* p);But the value of the pointer variable may be modified, so it can be used with const.

3. Functions are independent of each other. Sometimes collaboration requires shared variables (too many global variables will cause naming conflicts and will not be released to waste memory). Sharing can be achieved by passing the address of variables between functions. The effect of variables.

3. Usage of pointers:

Definition: Type* variable name_p;

1. Pointer variables and The default value of ordinary variables is uncertain and is generally initialized to NULL.

2. The usage of pointer variables is different from that of ordinary variables, and they are usually distinguished by ending with p.

3. The type of the pointer variable determines how many bytes are accessed when accessing memory through the pointer variable.

4. Pointer variables cannot be defined continuously (one * can only define one pointer variable):

int* p1,p2; // p is a pointer, p2 It is an int type variable

int *p1,*p2; // p1 and p2 are both pointer variables

Assignment: pointer variable = memory number. Variable name_p = address;

The memory number must be accessible. If the number is wrong, it may cause a segfault. void* can be automatically converted to any type of pointer (not in C). Make sure that the address corresponds to the physical memory (mapped), otherwise there will be a segfault.

 int* p = malloc(4);
 int* p = # // 引用内存

Access: *Pointer variable //Dereference----Access pointer variable

Access the corresponding memory according to the memory number stored in the pointer variable. If the previously assigned memory number is incorrect, a segmentation fault will occur in this step. The bytes accessed are determined by the pointer type. int* p = #                         *p 96b4fef55684b9312718d5de63fb7121 num;

5, pointer arithmetic

The pointer variable stores an integer, so it is an operator pointer variable that can be used for integer data Basically,

can be used, but not all operations are meaningful.

Pointer + integer = pointer width * integer

Pointer - integer = pointer - width * integer // The pointer moves forward and backward

Pointer - pointer = (pointer - pointer) /Width//How many elements are separated between two pointers

Pointer>, 91eef5681cad6f0cc96f9adeb2931b45= The pointer can determine which of the two is in front and who is behind.

6, Arrays and Pointers

. The array name is a special address and can also be used as a pointer. The array name is a constant (the array name corresponds to the first address of the first element of the array) relationship, ordinary pointers point to relationships). The array name can use pointer dereference, and the pointer variable can also use the array's [];arr[i] 96b4fef55684b9312718d5de63fb7121 *(arr i). When an array is used as a parameter of a function, it becomes a pointer variable. The length is lost and the security becomes smaller. void func(int * const arr, size_t len);

7, Advanced use of pointers

Array pointer and pointer array:

(1). Array pointer (pointer): a pointer specially used to point to an array.

int arr[10];
int (*p)[10] = arr;
int* p = #

void func(int I,int J,int(*arr)[J]) //Two-dimensional array function uses array pointer to pass parameters

(2 ).Pointer array (array): The data type stored in an array is a pointer.

together.

int* arr[3]; 96b4fef55684b9312718d5de63fb7121 int *p1,*p2,*p3;

(3). Secondary value needle: pointing Pointer to pointer

4. Function pointer: Pointer to function (cannot be dereferenced)

9, const pointer

const int * p; // The data in the memory pointed to by the pointer cannot be modified through dereference

(1) Parameters of the protected function

(2) When the pointer points to read-only data, it should also be added const prevents segfaults.

int const * p; // Same as above

int* const p; // The value of a pointer variable cannot be modified

 可以防止指针变量意外变成野指针

    const int* const p; // 既保存指针所指针的内存,也保护指针变量

    int const * const p; // 同上

运行:

*p_num=10
请输入一个数字:1
*p_num是1
*ptr=10

10,注意事项:

空指针:

  • 变量指针的值等于NULL,这种指针叫空指针。

  • 不能对空指针解引用,一定会出现段错误。

  • 当操作重启时会跳转NULL地址,进行重启,因此NULL存储操作系统用于重启的数据。

  • NULL在C语言中是一种错误标志,如果函数的返回值是指针类型,

  • 结果一旦NULL表示函数执行出错或失败。

如何避免空指针造成的段错误?

使用来历不明(函数的参数)的指针前先进行检查,if(NULL == p)。

野指针:指针变量的值不确定,使用野指针不一定会出错。

int* p; // 野指针

使用野指针的后果:段错误。

注意:野指针是无法分辨的,所以比空指针危害更。

如何避免野指针造成的错误?

  • 所有的野指针都人制造出来的,只要人人都不制造野指针就会有野指针造成的错误。

  • 定义指针变量时一定要初始化。

  • 指针变量所指向的内存初始释放后要及时赋值为空(堆内存)。

  • 不返回局部变量的地址。

相关学习推荐:C视频教程

The above is the detailed content of What are the usages of pointers in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn