C使用*param和&param调用函数

lil*_*lzz 2 c pointers

我见过处理*param1和¶m2的C函数调用

   func1(*param1);

   func2(&param2);
Run Code Online (Sandbox Code Playgroud)

我知道*和&必须用指针.那么这两种不同方式的目的是什么呢?每个人的任何优势?

Bob*_*oss 7

func1(*param1);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您将传递指针param1指向函数的地址的内容func1.

func2(&param2);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您将传递param2该函数的地址func2.

本质上,第二个创建一个新指针(即"查看那里!"),第一个指示指针指向的是什么(即"这个盒子里有什么?").

为了推动这一点,这是一个几乎没用的例子:

int x = 1234; /* an example value */
int *y = &x; /* The pointer y now points at x (or the memory location in which x resides). */
int z = *y; /* z is now = 1234 - it looked at what y was pointing at and took that value. */
int w = *(&x); /* w is now = 1234 - I created a temporary pointer and then immediately dereferenced it. */
Run Code Online (Sandbox Code Playgroud)

另外,请注意int *y指针定义:在变量定义期间,星形具有不同的含义.它用于定义指针而不是取消引用指针.对于新手而言有点令人困惑....