我刚开始讲指针,有点困惑。我知道&意味着一个变量的地址,*可以用在一个指针变量前面,以获得指针所指向的对象的值。但是当你使用数组、字符串或者使用变量的指针副本调用函数时,情况就不同了。很难看出其中的逻辑模式。

什么时候应该使用&和*?


当前回答

简单地说

&表示地址,你会看到在C语言中修改参数变量的函数占位符中,参数变量是按值传递的,使用&表示按引用传递。 *表示指针变量的解引用,即获取该指针变量的值。

int foo(int *x){
   *x++;
}

int main(int argc, char **argv){
   int y = 5;
   foo(&y);  // Now y is incremented and in scope here
   printf("value of y = %d\n", y); // output is 6
   /* ... */
}

上面的例子说明了如何使用引用传递来调用函数foo,与此比较

int foo(int x){
   x++;
}

int main(int argc, char **argv){
   int y = 5;
   foo(y);  // Now y is still 5
   printf("value of y = %d\n", y); // output is 5
   /* ... */
}

下面是使用解引用的示例

int main(int argc, char **argv){
   int y = 5;
   int *p = NULL;
   p = &y;
   printf("value of *p = %d\n", *p); // output is 5
}

上面说明了我们如何获得y的地址并将其赋值给指针变量p。然后我们通过将*附加到p的前面来解引用p,以获得p的值,即*p。

其他回答

我觉得你有点糊涂了。你应该读一本关于指针的教程/书。

本教程非常适合初学者(清楚地解释了&和*是什么)。别忘了读Kenneth Reek写的《C语言指针》

&和*之间的区别非常明显。

例子:

#include <stdio.h>

int main(){
  int x, *p;

  p = &x;         /* initialise pointer(take the address of x) */
  *p = 0;         /* set x to zero */
  printf("x is %d\n", x);
  printf("*p is %d\n", *p);

  *p += 1;        /* increment what p points to i.e x */
  printf("x is %d\n", x);

  (*p)++;         /* increment what p points to i.e x */
  printf("x is %d\n", x);

  return 0;
}

I was looking through all the wordy explanations so instead turned to a video from University of New South Wales for rescue.Here is the simple explanation: if we have a cell that has address x and value 7, the indirect way to ask for address of value 7 is &7 and the indirect way to ask for value at address x is *x.So (cell: x , value: 7) == (cell: &7 , value: *x) .Another way to look into it: John sits at 7th seat.The *7th seat will point to John and &John will give address/location of the 7th seat. This simple explanation helped me and hope it will help others as well. Here is the link for the excellent video: click here.

下面是另一个例子:

#include <stdio.h>

int main()
{ 
    int x;            /* A normal integer*/
    int *p;           /* A pointer to an integer ("*p" is an integer, so p
                       must be a pointer to an integer) */

    p = &x;           /* Read it, "assign the address of x to p" */
    scanf( "%d", &x );          /* Put a value in x, we could also use p here */
    printf( "%d\n", *p ); /* Note the use of the * to get the value */
    getchar();
}

附加组件:在使用指针之前总是初始化它们。否则,指针将指向任何东西,这可能导致程序崩溃,因为操作系统将阻止您访问它知道不属于您的内存。但是简单地输入p = &x;,我们为指针分配了一个特定的位置。

事实上,你已经掌握了,没有什么你需要知道的了:-)

我想补充以下几点:

这两种操作是截然相反的。&接受一个变量并给你地址,*接受一个地址并给你变量(或内容)。 将数组传递给函数时,数组“降级”为指针。 实际上你可以在间接上有多个层次(char **p表示p是指向一个char的指针的指针。

至于事情的运作方式不同,其实不是这样的:

如前所述,数组在传递给函数时降级为指针(指向数组中的第一个元素);它们不保存大小信息。 C语言中没有字符串,只有字符数组,按照惯例,表示以0(\0)字符结束的字符串。 当你将一个变量的地址传递给一个函数时,你可以去引用指针来改变变量本身(通常变量是按值传递的(数组除外))。

首先理解指针是复杂的,你必须做练习和 多多练习。 不要指望在第一次迭代时就能抓住它,也不要指望读到解释 并认为你已经理解了,因为很可能你没有……

如果你想要的不仅仅是理论上的理解,我建议遵循这个 斯坦福CS107课程和练习, 至少遵循前三节课,其中解释了指针。

斯坦福CS107, Jerry Cain

另一个非常有价值的工具是gdb,在那里您可以进行交互 就像你用python写的那样。 用gdb你可以玩和实验:

 (gdb) x pp.name
0x555555555060 <_start>:        0x8949ed31
(gdb) x &pp.name
0x7fffffffdc38: 0x55555060
(gdb) p &pp.name
$4 = (char **) 0x7fffffffdc38
(gdb) p *pp.name
$5 = 49 '1'

好吧,看起来你的帖子被编辑了…

double foo[4];
double *bar_1 = &foo[0];

看看如何使用&来获取数组结构开头的地址?以下

Foo_1(double *bar, int size){ return bar[size-1]; }
Foo_2(double bar[], int size){ return bar[size-1]; }

会做同样的事情。