"&的地址QUOT; (安培)的数组/地址被就是gcc忽略? [英] "Address of" (&) an array / address of being ignored be gcc?

查看:221
本文介绍了"&的地址QUOT; (安培)的数组/地址被就是gcc忽略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名编程课程的助教,有的学生做这种类型的错误:

I am a teaching assistant of a introductory programming course, and some students made this type of error:

char name[20];
scanf("%s",&name);

这是因为他们的学习也就不足为奇了......令人惊讶的是,除了海湾合作委员会警告后,code ++工程(至少这部分)。我一直在试图理解和我写了下面code:

which is not surprising as they are learning... What is surprising is that, besides gcc warning, the code works (at least this part). I have been trying to understand and I wrote the following code:

void foo(int *v1, int *v2) {
  if (v1 == v2)
    printf("Both pointers are the same\n");
  else
    printf("They are not the same\n");
}

int main() {
  int test[50];
  foo(&test, test);
  if (&test == test)
    printf("Both pointers are the same\n");
  else
    printf("They are not the same\n");
}

编译和执行:

$ gcc test.c -g
test.c: In function ‘main’:
test.c:12: warning: passing argument 1 of ‘foo’ from incompatible pointer type
test.c:13: warning: comparison of distinct pointer types lacks a cast
$ ./a.out 
Both pointers are the same
Both pointers are the same

任何人都可以解释为什么他们不一样?

Can anyone explain why they are not different?

我怀疑这是因为我不能让一个数组的地址(因为我不能让&放大器;&放大器; X ),但在这种情况下,code不应该编译。

I suspect it is because I cannot get the address of an array (as I cannot have & &x), but in this case the code should not compile.

编辑:我知道自身的数组是一样的第一个元素的地址,但这不是与此相关的问题,我想。例如:

I know that an array by itself is the same as the address of the first element, but this is not related to this problem, I think. For example:

int main() {
  int a[50];
  int * p = a;
  printf("%d %d %d\n", p == a, p == &a[0], &p[0] == a);
  printf("%d %d %d\n", p == &a, &p == a, &p == &a);
}

打印:

$ ./a.out 
1 1 1
1 0 0

我不明白,为什么第二行以 1 开始。

推荐答案

在您的例子中,数组测试是50 整数块。所以它看起来是这样的:

In your example, the array test is a block of 50 ints. So it looks like this:

| int | int | ... | int |

在应用一元&安培; 运营商数组,你得到的阵列的地址的。当你把它应用到别的只是想,真的。因此,&放大器;测试是指向50 整数该块的指针:

When you apply the unary & operator to an array, you get the address of the array. Just like when you apply it to anything else, really. So &test is a pointer that points to that block of 50 ints:

(&test) -----------> | int | int | ... | int |

指向50整数数组的类型为指针 INT(*)[50] - 这是&放大器的类型;测试

当你只需要使用名称测试在它不是操作数的任何地方无论是的sizeof 或一元 - &安培; 运营商,它是计算为一个指向它的第一个元素。因此,测试传递给富()计算为一个指向试验[ 0] 元素:

When you just use the name test in any place where it's not the operand of either the sizeof or unary-& operators, it is evaluated to a pointer to its first element. So the test that you pass to foo() evaluates to a pointer to the test[0] element:

(test) -----------------\
                        v
(&test) -----------> | int | int | ... | int |

您可以看到,这些都指向同一个地址 - 尽管&放大器;测试指向整个数组,而测试指向数组的第一个元素。

You can see that these both are pointing to the same address - although &test is pointing to the whole array, and test is pointing to the first element of the array (which only shows up in the different types that those values have).

这篇关于"&的地址QUOT; (安培)的数组/地址被就是gcc忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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