指向未指定大小的数组 [英] Pointer to array of unspecified size

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

问题描述

来自FAQ 6.13:


int arr [4] [5];

int(* a)[] = arr; / *合法,但没用* /


printf("%d \ n",a [0] [0]); / *错误,未指定的界限* /


是否有非人为的情况,其中一系列未指定的

界限是否有用?


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

From FAQ 6.13:

int arr[4][5];
int (*a)[]=arr; /* legal, but useless */

printf( "%d\n", a[0][0] ); /* error, bounds of a unspecified */

Are there non-contrived situations where an array of unspecified
bounds is useful?

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

推荐答案



" Christopher Benson-Manica" <在*** @ nospam.cyberspace.org> D'è?óê?t

新闻:bq ********** @ chessie.cirr.com ...

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> D′è?óê?t
news:bq**********@chessie.cirr.com...
来自FAQ 6.13:

int arr [4] [5];
int(* a)[] = arr; / *合法,但没用* /
~~~~~~~~~~~~~~这里错误无法转换为int [*] []来自int [] []


也许int * a []效果更好

for(i = 0; i< 4; i ++)

a [i] =& arr [i ] [0]

printf("%d \ n",a [0] [0]); / *错误,未指定的界限* /

是否有非人为的情况,其中一系列未指定的边界是有用的?

-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
From FAQ 6.13:

int arr[4][5];
int (*a)[]=arr; /* legal, but useless */ ~~~~~~~~~~~~~~here error cannot convert to int(*)[] from int[][]

maybe int *a[] work better
for( i=0;i<4;i++)
a[i]= &arr[i][0]

printf( "%d\n", a[0][0] ); /* error, bounds of a unspecified */

Are there non-contrived situations where an array of unspecified
bounds is useful?

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.



在< bq ********** @ chessie.cirr.com> Christopher Benson-Manica< at *** @ nospam.cyberspace.org>写道:
In <bq**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
来自FAQ 6.13:

int arr [4] [5];
int(* a)[] = arr; / *合法,但没用* /

printf("%d \ n",a [0] [0]); / *错误,未指定的边界* /


实际错误是你试图在指向不完整的指针上执行指针算术

类型,这在C中是不可能的。

是否有非人为的情况,其中一系列未指定的边界是有用的?
From FAQ 6.13:

int arr[4][5];
int (*a)[]=arr; /* legal, but useless */

printf( "%d\n", a[0][0] ); /* error, bounds of a unspecified */
The actual error is that you''re trying to perform pointer arithmetic
on a pointer to an incomplete type, which is impossible in C.
Are there non-contrived situations where an array of unspecified
bounds is useful?




其中很多,主要在< stdio.h>,< string.h>和< stdlib.h> ;.


或者,在C89中,没有VLA,使用数组定义的唯一方法

in different模块,其大小在编译时是未知的:


extern char buff [];

extern size_t buffsize;


OTOH,如果你在谈论指向未指定大小的数组的指针,那么它们是没用的,因为指针算术不会对它们起作用。一个

指向未指定大小的数组的第一个元素,这是我上面例子中的类型buff衰减到的b / b $,它更有用。


Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da*****@ifh.de


Dan Pop< Da ***** @ cern.ch>这样说:
Dan Pop <Da*****@cern.ch> spoke thus:
实际的错误是你试图对指向不完整类型的指针执行指针运算,这在C. $ b $中是不可能的。 b

谢谢。

其中很多,主要在< stdio.h>,< string.h>和< stdlib.h>。


你能举个例子说明你在说什么吗?我通过< stdio.h>浏览了一下

和< string.h>并没有看到任何看起来像我们正在讨论的东西。

OTOH,如果你在谈论指向未指定大小的数组的指针,


是的。

他们没用,因为指针算法不适用于他们。
The actual error is that you''re trying to perform pointer arithmetic
on a pointer to an incomplete type, which is impossible in C.
Thanks.
Plenty of them, mostly in <stdio.h>, <string.h> and <stdlib.h>.
Can you give me an example of what you''re talking about? I glanced
through <stdio.h> and <string.h> and didn''t see anything that looked
like what we''re discussing.
OTOH, if you''re talking about pointers to arrays of unspecified size,
Yes.
they''re useless because pointer arithmetic doesn''t work on them.




我想的多了,这引出了我真正的问题:如果它是完全无用的,那为什么要使它成为合法的语法?对于那些没有读过的人来说这是一个问题6.13(我确定这样的人存在),这似乎很诱人

声明(* a)[]和然后尝试使用它。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



I figured as much, which leads me to my real question: If it''s
wholly useless, why make it legal syntax? To someone who hasn''t read
FAQ 6.13 (I''m sure such people exist), it might seem tempting to
declare (*a)[] and then attempt to use it.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


这篇关于指向未指定大小的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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