前置声明

Lactoferrin 2012-03-04 11:31:59
static char s[];

void proc()
{puts(s);}

static char s[]="aaa";

这样是否规范?有的编译器认有的不认
...全文
332 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lactoferrin 2012-03-06
  • 打赏
  • 举报
回复
static 就不能是声明?
没有蜡 2012-03-06
  • 打赏
  • 举报
回复
LZ,extern int a[]并不是一个变量的定义,他仅仅是一个声明,不分配内存;

Lactoferrin 2012-03-05
  • 打赏
  • 举报
回复
不是没有大小的问题,应该是静态对象不能前置声明
换成extern就没问题
bluewanderer 2012-03-05
  • 打赏
  • 举报
回复
gcc 4.4.3 C没问题

#include <stdio.h>

static int a[];

int main()
{
a[0] = 0;

return sizeof(a); // 只是不能取size
}

static int a[10];

----

test.c: In function ‘main’:
test.c:9: error: invalid application of ‘sizeof’ to incomplete type ‘int[]’

不过C++只有VC上能过
自信男孩 2012-03-05
  • 打赏
  • 举报
回复
static char s[];
这样好像不行,数组大小可以不确定吗?即使没有大小,那也在后面初始化吧。
Lactoferrin 2012-03-05
  • 打赏
  • 举报
回复
把static换成extern就可以
这里不需要知道大小 puts只需要地址
Lactoferrin 2012-03-05
  • 打赏
  • 举报
回复
我这里32位vc 32位gcc 64位gcc认
但64位vc不认
ouyh12345 2012-03-05
  • 打赏
  • 举报
回复
g++ 4.1.2
不认
error: storage size of 's' isn't known
wizard_tiger 2012-03-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 anyidan 的回复:]
static char s[]; 肯定不行
[/Quote]
++
数组必须先确定大小。
东莞某某某 2012-03-05
  • 打赏
  • 举报
回复
试想 即便可以怎么使用呀,没有内存大小。
AnYidan 2012-03-05
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lactoferrin 的回复:]
把static换成extern就可以
这里不需要知道大小 puts只需要地址
[/Quote]

的确可以, 但
win7 + vs2010(32-bit) 下, static char s[]; 提示数组长度没有指定
bluewanderer 2012-03-05
  • 打赏
  • 举报
回复
4 If the size is not present, the array type is an incomplete type. If the size is * instead of
being an expression, the array type is a variable length array type of unspecified size,
which can only be used in declarations with function prototype scope;
122)
such arrays are
nonetheless complete types. If the size is an integer constant expression and the element ype has a known constant size, the array type is not a variable length array type;
otherwise, the array type is a variable length array type.

8 EXAMPLE 2 Note the distinction between the declarations
extern int *x;
extern int y[];
The first declares x to be a pointer to int; the second declares y to be an array of int of unspecified size
(an incomplete type), the storage for which is defined elsewhere.
northcan 2012-03-05
  • 打赏
  • 举报
回复
static char s[];

应该不行吧,数组长度未知。
AnYidan 2012-03-05
  • 打赏
  • 举报
回复
static char s[]; 肯定不行
吾子墨鸿 2012-03-05
  • 打赏
  • 举报
回复
这个和编译器有关系。但是实际应该避免这样写,一个这样根本就不规范,另外也很难移植!
guchang 2012-03-05
  • 打赏
  • 举报
回复
不行,避免这么写
面包大师 2012-03-05
  • 打赏
  • 举报
回复
不行。。。
bluewanderer 2012-03-04
  • 打赏
  • 举报
回复
至少是符合现在规范的,类型是incomplete type,和声明结构体大体是同一种处理,不能取长度或者进行涉及长度的操作。
merlinfang 2012-03-04
  • 打赏
  • 举报
回复
不规范,前置申明,引用的肯定要知道具体大小的

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧