[英]Syntax explanation with pointers as function parameters (C++)
I'm not sure if I can ask basic questions here, but I am starting to learn C++ and do not understand one little thing in pointers syntax. 我不确定我是否可以在这里提出基本问题,但我开始学习C ++并且在指针语法中不理解一件小事。
Here is the sample of my code: 以下是我的代码示例:
using namespace std;
int randomname(int *x);
int main(){
int a = 1;
int *ab;
ab = &a;
randomname(&a);
}
int randomname(int *x){
*x = 9001;
}
My question is about the * symbol. 我的问题是关于*符号。 Why in the main function on line ab = &a;
为什么在主要功能上行ab =&a; I don't need the *, but on line *x = 9001;
我不需要*,但在线* x = 9001; I need it?
我需要它? I think syntax should be same in both functions, but it isn't.
我认为两种函数的语法应该相同,但事实并非如此。 Can someone please explain why?
有人可以解释一下原因吗?
The meaning of both the asterisk *
and the ampersand &
changes depending on the context. 两个星号的含义
*
和符号&
根据上下文的变化。 Their meanings in expressions and in declarations are different: 它们在表达式和声明中的含义是不同的:
*
is used in a declaration , it designates a pointer *
,它指定一个指针 &
is used in a declaration , it designates a reference &
在声明中使用时,它指定一个引用 *
is used in an expression , it performs a pointer dereference of its operand *
,它执行其操作数的指针解除引用 &
is used in an expression , it obtains a pointer of its operand &
在表达式中使用时,它获得其操作数的指针 Once you understand these distinctions, you can tell that 一旦你理解了这些区别,你就可以说出来
int *ab
is a declaration . int *ab
是一个声明 。 Asterisk designates ab
as a pointer. ab
指定为指针。 ab = &a;
ab = &a;
has an expression . &
takes a
's pointer, and assigns it to ab
, which has a pointer type specified at the time of its declaration (above). &
获取a
指针,并将其分配给ab
,它具有在声明时指定的指针类型(上图)。 *x = 9001
is also an expression , making the asterisk a dereference operator. *x = 9001
也是一个表达式 ,使星号成为解除引用运算符。 You use the asterisk to tell the compiler that the target of the assignment is whatever is pointed to by x
, not x
itself. x
所指向的内容,而不是x
本身。 You need the asterisk when you are declaring a pointer, or when you are dereferencing the pointer. 在声明指针时,或者在取消引用指针时,需要星号。 The syntax actually is the same in both functions, but what is being assigned in each of the assignment statements is different.
两个函数中的语法实际上是相同的,但是在每个赋值语句中分配的内容是不同的。
An int
is a value. int
是一个值。 An int
variable is stored in a storage location. int
变量存储在存储位置。 A pointer to an int
is also a value, but its value refers to a storage location. 指向
int
的指针也是一个值,但其值指的是存储位置。 When you are performing an assignment with a pointer, you could assign the value of the pointer itself (the address of the storage location it refers to), or you could assign a value to the storage location it refers to. 使用指针执行赋值时,可以指定指针本身的值(它所引用的存储位置的地址),或者可以为它引用的存储位置赋值。
In the case of int *x
in the formal parameters of the function, or in the declaration int *ab
that defines the ab
variable, you need the asterisk because you are declaring a variable or a parameter to be of type "pointer to int". 对于函数的形式参数中的
int *x
,或者在定义ab
变量的声明int *ab
中,您需要星号,因为您声明变量或参数类型为“指向int的指针” 。
In the assignment ab = &a
you do not need the asterisk because you are assigning the address of variable a
to the pointer ab
-- you are assigning the value of the pointer itself, not what it points to. 在赋值
ab = &a
您不需要星号,因为您要将变量a
的地址赋给指针ab
- 您指定的是指针本身的值,而不是它指向的值。
In the assignment *x = 9001
, you need the asterisk to get the storage location that the pointer refers to. 在赋值
*x = 9001
,您需要星号来获取指针所指的存储位置。 This is called "dereferencing" the pointer. 这称为“解除引用”指针。 You are not assigning a value to the pointer itself, but instead you are assigning a value to the storage location to which the pointer refers, which is the storage allocated for variable
a
in function main
. 您没有为指针本身赋值,而是将值赋给指针所引用的存储位置,这是为函数
main
变量a
分配的存储。
This article by Eric Lippert may be of considerable help to you: Eric Lippert撰写的这篇文章对您有很大的帮助:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.