C++ 中的指针和函数
来自大学一门关于“按值调用”的课程的讲义:
void fun(int *ip) { *ip =100; }
打电话给
int n=2; int *np; np = &n; 有趣(np);
将 n 的值更改为 100。
当我们说“int *ip”时,我们到底是什么意思?整数类型的指针?如果是这样,当我们以 np 作为参数调用 fun() 时,是否应该出现错误,因为 np 具有 n 的地址,而 n 不是整数?
然后,我们将 ip 的值更改为 100,那么这是否意味着 n 现在具有地址为 100 的“内存槽”中的值?我确信我错过了一些东西。 :)
From the lectures notes of a course at university, on "call-by-value":
void fun(int *ip) { *ip =100; }
called by
int n=2; int *np; np = &n; fun(np);
would change the value of n to 100.
When we say "int *ip", what exactly do we mean? A pointer of type integer? If so, when we call fun() with np as its argument, shouldn't there be an error as np has the address of n, which is not an integer?
And then, we change the value of ip to 100, so doesn't that mean that n now has the value that's in the "memory slot" with the address 100? I am sure I am missing something. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不,是一个指向整数的指针。
n
是一个整数,所以没有问题。&n
、np
和ip
在代码中都具有相同的类型:int*
。否...我们更改
*ip
的值,而不是ip
的值。也就是说,我们更改ip
指向的值(有时也称为pointee)。No, a pointer to an integer.
n
is an integer so there’s no problem.&n
,np
andip
all have the same type in your code:int*
.No … we change the value of
*ip
, not ofip
. That is, we change the value thatip
points to (which is also sometimes called the pointee).是的,当您使用
int *
作为fun
的参数类型时,您是在说它接受一个指向 int 的指针。由于np
是一个指向int的指针,将其传递给fun
是可以的。np
和ip
指向n
的地址,因此当您分配像*ip = 42
这样的值时,它会将值 42 分配给ip
所指向的内存位置 - 即n
。Yes, when you use
int *
as a parameter type offun
you are saying it accepts a pointer to an int. Sincenp
is a pointer to an int passing it intofun
is ok.np
andip
are pointing to the address ofn
, so when you assign a value like*ip = 42
, it will assign the value of 42 to the memory location thatip
is pointing to - which isn
.示例代码中的内容称为通过指针传递。您的函数有一个整数指针类型的参数。因此传递给函数的值是变量
n
的地址。该函数将100
分配给指针参数ip
指向的变量(即内存位置)。星号*
是 C/C++ 中的解引用运算符。What you have in the sample code is called pass by pointer. Your function has a parameter of type pointer to integer. So the value passed to the function is the address of the variable
n
. The function assigns100
to the variable (i.e. a memory location) pointed to by the pointer parameterip
. The star*
is the dereference operator in C/C++.在 fun() 中,您不是将 ip 的值更改为 100,而是更改 ip 指向的内存位置。
in fun(), you aren't changing the value of ip to 100, you're changing the at the memory location pointed to by ip.
type*
用作类型表示“指向该类型的指针”,常见用法是
int *var
,但我喜欢将其写为int* var
code> 因为这使得它与取消引用指针(即*ptr
)有更好的区别。所以
int* a
表示,a 是指向 int 类型对象的指针,*a
表示 a 和&a
指向的对象> 表示对象a的地址。type*
used as a type means "a pointer to that type"common use is
int *var
, but I like to write it asint* var
as that makes it a better distinction from dereferenceing a pointer, i.e.*ptr
.so
int* a
means, a is a pointer to an object of type int,*a
means the object pointed to by a and&a
means the address of object a.将
np
视为int*
类型。请记住,为此目的,星号和 int 一起构成“指向 int 的指针”类型,并且不可分离。think of
np
as of typeint*
. Bear in mind that for this purpose the asterisk and theint
together make a "pointer to int" type, and are not separatable.ip
是一个指针,通过ip*
您可以访问它指向的“内存槽”。np
也是一个指针。使用&n
将n
的内存槽地址分配给np
。因此,当调用fun()
时,在fun()
内部,唯一访问的内存槽是n
之一,因此访问n
已分配 100。ip
is a pointer, withip*
you access the "memory slot" it's pointing to.np
is also a pointer. Using&n
you assign the address of the memory slot ofn
tonp
. So when callingfun()
, insidefun()
the only memory slot that's accessed is the one ofn
and thus ton
100 is assigned.这里可能会出现一些混淆,因为 n 和 np 都存储数字 - 但是,在编译时,编译器会以不同的方式使用这些数字;也就是说,虽然
和
都是算术运算,但生成的汇编是不同的。重要的是要记住,归根结底,计算机中的所有数据都是数字。它们成为不同类型的数据仅仅是因为我们以不同的方式对待它们。
具体到您的示例,
您需要记住
*
表示取消引用,并且该操作发生在赋值之前。用多余的括号可能会更清楚:或者在不同的上下文中:
现在,我必须说,当你说时,它温暖了我的心,
这会做你所描述的事情,因为它告诉计算机将 np 的数字视为不同的类型数量;就像 static_cast(np) 将 np 指向的数字视为字符而不是整数一样。
Some confusion is possible here because both n and np store numbers - however, when compiling, the compiler will use the numbers differently; that is, while
and
are both actually arithmetic operations, the generated assembly is different. It is important to remember that, ultimately, all of the data in the computer are numbers. They become different types of data merely because we treat them differently.
Specifically with regards to your example,
you need to remember that the
*
means dereference, and that operation happens before the assignment. It may be clearer with superfluous parenthesis:or in a different context:
Now, I must say, it warms my heart when you say,
This would do what you described, because it tells the computer to treat the number that is np as a different kind of number; in the same way that
static_cast<char*>(np)
would treat the number np points to as a character, rather than as an integer.