这是C $ C $Ç错了吗? [英] Is this C code wrong?

查看:138
本文介绍了这是C $ C $Ç错了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,含有指针变量的地址,例如:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆