参考阵VS引用数组指针 [英] Reference to Array vs reference to array pointer

查看:143
本文介绍了参考阵VS引用数组指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void check(void* elemAddr){
    char* word = *((char**)elemAddr);
    printf("word is %s\n",word);
}

int main(){
    char array[10] = {'j','o','h','n'};
    char * bla = array;
    check(&bla);
    check(&array);
}

输出:

word is john

RUN FINISHED; Segmentation fault; core dumped;

第一个作品,但第二不行。我不明白为什么会这样。

First one works, but second not. I don't understand why this happens.

推荐答案

在C规范表示,阵列和&安培;阵列是相同的指针地址

The C specification says that array and &array are the same pointer address.

使用数组的名字传递一个数组时,函数会自动将参数转换成按照C规范(重点煤矿)的指针。

Using the name of an array when passing an array to a function will automatically convert the argument to a pointer per the C specification (emphasis mine).

6.3.2.1-4

6.3.2.1-4

除了当它是sizeof操作符或一元和放大器的操作;
  运营商,或者是用于初始化数组文本字符串,
  具有类型'类型的阵列'前pression被转换为
  前pression型指向最初的指针键入''
  数组对象
并的元素不是一个左值。如果数组对象
  已注册存储类,行为是不确定的。

Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

所以调用FUNC(阵列)将导致一个字符指针[]被传递给函数。但对于使用的阵列上的地址的操作者的一个特例。由于阵列的类型类型的数组它落入规范(重点煤矿)的,否则类别。

So calling func(array) will cause a pointer to char[] to be passed to the function. But there is a special case for using the address-of operator on an array. Since array has type "array of type" it falls into the 'Otherwise' category of the specification (emphasis mine).

6.5.3.2-3

6.5.3.2-3

单目&放大器;运营商产生操作数的地址。如果操作数
  已键入''类型'',结果类型为'指向类型'。如果
  操作数是一元*运算符的结果,既没有运营商也没有
  在&功放;操作员进行评价,其结果是,如果两者都省略,
  除了对经营者的约束仍然适用和
  结果不是一个左值。类似地,如果操作数是一个结果
  []运营商,无论是与放大器;运营商也不是隐含了一元*
  在[]的条件判断,其结果是,如果在&放大器;运营商分别为
  删除,[]操作改为A +运营商。 ,否则,
  结果是一个指针所指定的对象或函数将其
  操作

The unary & operator yields the address of its operand. If the operand has type ‘‘type’’, the result has type ‘‘pointer to type’’. If the operand is the result of a unary * operator, neither that operator nor the & operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue. Similarly, if the operand is the result of a [] operator, neither the & operator nor the unary * that is implied by the [] is evaluated and the result is as if the & operator were removed and the [] operator were changed to a + operator. Otherwise, the result is a pointer to the object or function designated by its operand

所以调用FUNC(安培;阵列)仍将导致一个单指针被传递给函数一样调用FUNC(阵列)不会因为两个阵列和&放大器;阵列是相同的指针值。

So calling func(&array) will still cause a single pointer to be passed to the function just like calling func(array) does since both array and &array are the same pointer value.

常识性的会导致你相信和放大器;阵列是一个双指针数组的第一个元素,因为使用&放大器;操作者通常表现的方式。但是阵列是不同的。所以,当你去引用传递的数组指针双指针你得到一个分割故障的阵列。

Common-sense would lead you to believe that &array is a double pointer to the first element of the array because using the & operator typically behaves that way. But arrays are different. So when you de-reference the passed array pointer as a double pointer to the array you get a Segmentation fault.

这篇关于参考阵VS引用数组指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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