这是C $ C $Ç错了吗? [英] Is this C code wrong?
问题描述
我知道,含有指针变量的地址,例如:
I know that pointers contain the addresses of variables, for example:
int c = 5;
int *p;
p = &c;
printf("%d",*p); // Outputs 5.
但是,如果我想要一个地址发送到一个功能是什么:
But what if I want to send an address to a function:
void function (int *p)
{
p++;
}
int c;
function (&c);
当函数被调用时,&安培值;ç
分配给为int * P
。我想,确切的指令是: * P =和C;
,但我不明白这是什么意思
When function is called, the value of &c
is assigned to int *p
. I guess that the exact instruction is: *p = &c;
, but I don't understand what this means.
推荐答案
我preFER写为int * p =和C;
,即一个指针类型为int被分配C的地址。
I prefer to write int* p = &c;
, that is, a pointer to type int is being assigned the address of c.
指针符号可能会造成混淆,因为你已经注意到了,因为这两者是等价的:
Pointer notation can be confusing, as you've noticed, because these two are equivalent:
int *p = &c;
和
int *p;
p = &c;
希望这有助于!
编辑:混淆的来源,因为 *
同时用于作为引用操作,并宣告一个指针。因此,当 P
包含的C该地址
, * P
取消引用指针,并返回的ç
的值。但是,当你说为int * p =和C;
的 *
是说p是一个指向INT,但没有做任何取消引用。
The confusion comes because *
is used both as the dereference operator, and for declaring a pointer. So when p
contains the address of c
, *p
dereferences the pointer and returns the value of c
. But when you say int *p = &c;
, the *
is saying "p is a pointer to an int", but is not doing any dereferencing.
这篇关于这是C $ C $Ç错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!