我正在学习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)
都是正确的,对吗?有什么区别?
void funct(int &a)
在C语言中是无效的,但在C++中是有效的,就像int &c;
一样;因此你的问题有些混淆。 - Cliffordint &c;
不是有效的 C++ 代码。 - emlai