ansi C - 将值传递给数组

时间:2013-03-29 16:28:16

标签: arrays function

如果您调用的函数如下:

coworkers = engineers(1,0,3);
...

int engineers(unsigned char nerd, unsigned char dweeb[], unsigned char derp) {
     printf("Is this the address of dweeb[0]? 0x%x \n", dweeb);
     printf("Is this the value of dweeb[0]? %x \n", dweeb[0]);
} 

Output:
Is this the address of dweeb[0]? 0xfeedbeef
Is this the value of dweeb[0]? 

当我将0值传递给dweeb []时,它会变成dweeb [0]的值吗?

- 我是derp,周五快乐。感谢

1 个答案:

答案 0 :(得分:0)

dweeb[0]相当于*(dweeb+0)所以是的,它确实成为dweeb[0]的值。

请注意,因为dweeb + 0 = 0 + dweeb,您也可以将其写为0[dweeb]


仔细看看,当您将0作为第二个参数传递给engineers函数时,会发生的是地址0x0将被使用作为数组dweeb的位置。取消引用该地址(如dweeb[0]中所示)是未定义的行为。最可能的结果是程序崩溃。您实际看到的内容更有可能:

Is this the address of dweeb[0]? 0x0
Segmentation fault

我不确定你在问什么,如果是“如果我将0放在dweep[ ]”中的括号之间会发生什么,或者“如果我将0传递给dweep会发生什么? {{1}}“

的值