不完全数组类型?

编程入门 行业动态 更新时间:2024-10-24 00:31:23
本文介绍了不完全数组类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我整理了以下code。与的gcc -Wall -pedantic -ansi -std = C89 ,它成功编译没有在指针赋值给了一个错误。请注意,我从 INT(*)[4] 到 INT(*)[] 。

When I compiled the following code with gcc -Wall -pedantic -ansi -std=c89, it compiled successfully without giving an error at the pointer assignment. Note that I convert from int (*)[4] to int (*)[].

int arr[4]; int (*p_arr)[] = &arr;

假设有一些原因让这(?不兼容)分配的,当我尝试使用它,编译器为不完全型误差错误:'sizeof的不完全类型'诠释无效申请[]'。

(void) sizeof(*p_arr);

这个错误让我觉得有什么用,允许previous指针赋值 p_arr =放大器;改编?这是允许的分配按照标准?

This error makes me think what is the use of allowing the previous pointer assignment p_arr = &arr? Is this assignment allowed as per the standard?

我已经使用(通常为向前声明)不完整的struct / union类型,也遇到了误差不完整的数组元素类型。但这不完全数组类型是新的我。是否有可能在C标准,具有一个用例?

I have used incomplete struct/union type (usually for forward declaration) and also come across the error incomplete array element type. But this incomplete array type is new to me. Is it possible in C standard and has a use case?

推荐答案

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

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; }

更多推荐

不完全数组类型?

本文发布于:2023-11-27 08:59:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637395.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   不完全   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!