Home > Backend Development > C#.Net Tutorial > The latest summary of matters related to the understanding and use of pointers in C language

The latest summary of matters related to the understanding and use of pointers in C language

php是最好的语言
Release: 2018-07-26 18:11:57
Original
1741 people have browsed it

Definition: A pointer is a variable whose value is the address of another variable. The address represents the location in memory. What needs to be remembered is that the array variable itself is a pointer.

Type of address

Addresses have types. Doesn’t it feel strange? Doesn’t a pointer represent an address? Does an address also have a type? Look at an example:

int    *ip;    /* 一个整型的指针 */

double *dp;    /* 一个 double 型的指针 */

float  *fp;    /* 一个浮点型的指针 */

char   *ch;     /* 一个字符型的指针 */
Copy after login

In fact, a pointer is always just a hexadecimal number representing an address. The so-called type refers to the type of the variable pointed by the pointer.

Using pointers

How to define a pointer, you should know from the previous example, then how to print the hexadecimal address and or the data pointed to by the pointer:

//通过&运算符获取了i的地址并保存到intP中去

int *intP; = &i;

printf("intP存储的地址为:%p,存储的地址指向的数据为:%d\n", intP, *intP);
Copy after login

Pointers can perform operations: , --, , -

In addition, pointers can also be compared using relational operators, such as ==, < and >

int intArr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

//先定义一个指针执行数组第一个元素

int *intArrP = &intArr[0];

printf("此时intArrP存储的地址为:%p,数据为:%d\n", intArrP, *intArrP);

//自增一下看看结果(每增加一次,它都将指向下一个整数位置)

intArrP++;

printf("自增以后intArrP存储的地址为:%p,数据为:%d\n", intArrP, *intArrP);
Copy after login

Pointers You can also point to pointers

int data = 5201314;

int *p1 = &data;

int **p2 = &p1;

printf("%d\n", data); //都是5201314

printf("%d\n", *p1);

printf("%d\n", **p2);
Copy after login

Structures and pointers

The use of pointers in structures is a little special, mainly because of the particularity of the structure itself, and you want to use a value in the structure , generally divided into two situations: the structure itself and the pointer pointing to the structure. See the following example for details:

struct Node

{

    int val;
    
};

//先建立一个结构体数据

struct Node node;

node.val = 1;

struct Node *nodeP; //创建一个指向刚刚的结构体的指针

nodeP = &node;

printf("%d\n", nodeP->val);//指向结构体的指针用->

printf("%d\n", node.val);//结构体自身用.
Copy after login

Related articles:

The above is the detailed content of The latest summary of matters related to the understanding and use of pointers in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template