且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

不完全数组类型?

更新时间:2023-11-10 10:33:34

由于它们的类型是兼容的,因为的未知边界数组是兼容的元素类型的任何数组兼容该分配没有给出任何错误。仅供参考) -

This assignment didn't give any error because their types are compatible because arrays of unknown bound are compatible with any array of compatible element type. (For reference)-

int (*p_arr)[] = &arr;

但给错误的将它作为操作数的sizeof 运算符,因为 * p_arr 是不完整的类型的,你是不应该使用不完全类型的操作数,的sizeof 运营商。

But gives error on passing it as operand to sizeof operator because *p_arr is of incomplete type and you are not supposed to use incomplete types as operands to sizeof operator.

N1570 6.5.3.4

1 sizeof操作符的不得适用以具有函数类型或不完全类型的前pression,这种类型的括号中的名字,或一个前pression,指定位字段成员[...]

1 The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member[...].

现在你可以使用它,这里是一个简单的例子 -

Now what you can use it, here is a simple example -

#include <stdio.h>

int main(void){
    int arr[4]={1,2,3,4};
    int a[6]={1,2,3,3,1,1}; 
    int (*p_arr)[] = &arr;
    for(int i=0;i<4;i++)
       printf("%d",(*p_arr)[i]);
    printf("\n");
    p_arr=&a;
    for(int i=0;i<6;i++)
       printf("%d",(*p_arr)[i]);
    return 0;
}