【发布时间】:2015-04-30 23:09:55
【问题描述】:
我正在学习 C,但我仍然不确定我是否理解 & 和 * 之间的区别。
请允许我解释一下:
int a; // Declares a variable
int *b; // Declares a pointer
int &c; // Not possible
a = 10;
b = &a; // b gets the address of a
*b = 20; // a now has the value 20
我得到了这些,但后来变得混乱。
void funct(int a) // A declaration of a function, a is declared
void funct(int *a) // a is declared as a pointer
void funct(int &a) // a now receives only pointers (address)
funct(a) // Creates a copy of a
funct(*a) // Uses a pointer, can create a pointer of a pointer in some cases
funct(&a) // Sends an address of a pointer
那么,funct(*a) 和 funct(&a) 都是正确的,对吧?有什么区别?
【问题讨论】:
-
当您的问题不是专门针对该语言时,您不应使用多种语言进行标记。您的
// Not possible评论完全取决于语言。 -
我认为这个问题适用于 C 和 C++
-
您不能在 C 中将变量作为引用传递。
-
void funct(int &a)也不是有效的 C,它是有效的 C++ 和int &c;;所以你的问题有点困惑。 -
@Clifford
int &c;不是有效的 C++。
标签: c++ c pointers dereference