C创建指针

Creating Pointers

创建指针

You learned from the previous chapter, that we can get the memory address of a variable with the reference operator &:

通过前面的学习,我们知道了如何使用引用符号 & 来获取一个变量的地址:

Example 比如

int myAge = 43; // an int variable 一个整型变量

printf("%d", myAge); // Outputs the value of myAge (43) 输出myAge的值(43)
printf("%p", &myAge); // Outputs the memory address of myAge(0x7ffe5367e044)输出myAge的内存地址
In the example above, &myAge is also known as a pointer.

在上面的例子里,&myAge也被称为一个指针。

A pointer is a variable that stores the memory address of another variable as its value.

指针是一种存储 其他变量的内存地址 作为它的值的变量。

A pointer variable points to a data type (like int) of the same type, and is created with the * operator. The address of the variable you are working with is assigned to the pointer:

指针变量指向与之相同数据类型的数据(如 int ),并且指针是通过 * 运算符创建的。

Example 例如

int myAge = 43; // An int variable 一个整型变量
int* ptr = &myAge; // A pointer variable, with the name ptr, that stores the address of myAge 一个存储着myAge的地址,被称为ptr的指针变量

// Output the value of myAge (43)输出myAge的值(43)
printf("%d\n", myAge);

// Output the memory address of myAge (0x7ffe5367e044)输出myAge的内存地址
printf("%p\n", &myAge);

// Output the memory address of myAge with the pointer (0x7ffe5367e044)使用该指针输出myAge的内存地址
printf("%p\n", ptr);
Example explained 例子解释

Create a pointer variable with the name ptr, that points to an int variable (myAge). Note that the type of the pointer has to match the type of the variable you’re working with.

创建了一个名为ptr的指针变量,并指向了一个整型变量(myAge)。注意!指针的类型必须和指向变量的类型相对应!

Use the & operator to store the memory address of the myAge variable, and assign it to the pointer.

Now, ptr holds the value of myAge’s memory address.

使用 & 运算符获取myAge变量的内存地址,并传给指针。

现在,ptr 掌握着myAge的内存地址。

Dereference

解引用

In the example above, we used the pointer variable to get the memory address of a variable (used together with the & reference operator).

在上面的例子里,我们使用了指针取获取变量的内存地址(跟 & 运算符一起)

However, you can also get the value of the variable the pointer points to, by using the * operator (the dereference operator):

但是,通过 * 运算符(解引用运算符),你也可以获取指针指向变量的值

Example 示例

int myAge = 43; // Variable declaration 声明变量
int* ptr = &myAge; // Pointer declaration 声明指针

// Reference: Output the memory address of myAge with the pointer (0x7ffe5367e044)
//引用:用指针输出myAge的内存地址
printf("%p\n", ptr);

// Dereference: Output the value of myAge with the pointer (43)
//解引用:用指针输出myAge的值(43)
printf("%d\n", *ptr);
Note that the * sign can be confusing here, as it does two different things in our code:

When used in declaration (int* ptr), it creates a pointer variable.
When not used in declaration, it act as a dereference operator.

注意,这里 * 的使用可能会让你感到困惑,因为这个符号在我们的代码里有两个含义:

当用于声明时,它创建了一个指针变量(int* ptr)。

当不用于声明时,它起到解引用的作用。

Good To Know: There are three ways to declare pointer variables, but the first way is mostly used:

博识乃善:你知道声明变量有几种写法吗?有三种,第一种最常用:

int* myNum; // Most used 最常用
int *myNum;
int * myNum;
Why Should I Learn About Pointers?

为啥要学指针?

Pointers are one of the things that make C stand out from other programming languages, like Python and Java.

指针是让C语言在茫茫语海(如Python,Java)中脱颖而出的法宝。

This chapter was just a short introduction to Pointers. They are important in C, because they give you the ability to manipulate the data in the computer’s memory - this can reduce the code and improve the performance. However, pointers must be handled with care, since it is possible to damage data stored in other memory addresses.

本章只是对指针的简短介绍。指针在C语言中可重要了!因为它可以赋予你掌控电脑内存里的数据,还可以使代码更简洁高效。但是!它也可能损坏存储于其他内存的数据,必须谨慎使用!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值