ISO C90禁止因为数组的混合声明和code。我该如何解决这个问题? [英] ISO C90 forbids mixed declarations and code because of arrays. How do I fix this?

查看:846
本文介绍了ISO C90禁止因为数组的混合声明和code。我该如何解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用它编译 -gcc 和它的工作如预期,但与 -pedantic 添加时,它赢得T编译。我仍然在相当编程初学者,这是我第一次遇到这个问题,所以这对我来说是相当有问题。

I tried compiling it using -gcc and it worked as intended but when added with -pedantic, it won't compile. I'm still quite a beginner in programming and it is the first time I encountered this problem so it is quite a problem to me.

下面是code,它导致错误:

Here is the code that is causing the error:

char *exercise[5]={"swimming", "running", "brisk walking", "weight lifting", "zumba"};

我倒是AP preciate如果你能解释一下解决的办法是什么,而不只是固定code,因为我要学习。

I'd appreciate it if you would explain what the solution is instead of just the fixed code because I want to learn.

推荐答案

这无关与阵列具体。最初的版本标准C语言(ISO C90)禁止混合声明和code。

This has nothing to do with arrays specifically. The original version of standardized C language (ISO C90) forbids mixing declarations and code.

在C90的每个局部块由包围{} 有相当严格的结构:它声明的开头(如果有的话),然后再接着陈述(code )。

In C90 each local block surrounded by {} has rather strict structure: it begins with declarations (if any), which are then followed by statements (code).

这是你必须遵循的格式。移动阵列声明块的顶部。它应该是微不足道的,因为没有你的初始化依赖于任何运行时间的计算。这是所有有给它。

That is the format you have to follow. Move your array declaration to the top of the block. It should be trivial, since none of your initializers depend on any run-time calculations. That's all there is to it.

{
  /* Declarations go here */
  char *exercise[5]={"swimming", "running", "brisk walking", "weight lifting", "zumba"};

  /* Statements (i.e. code) goes here */
}

当然,这里的潜的问题是:你真的必须使用C90?是你明确要求写你的code为C90编译​​器?也许你应该你的编译器只需切换到C99模式,而忘记了这个特定的C90-限制?

Of course, the unspoken question here is: do you really have to use C90? Were you explicitly asked to write your code for C90 compiler? Maybe you should simply switch your compiler to C99 mode and forget about this C90-specific restriction?

在C语言新版本(C99和更高版本),你可以自由组合语句和声明。 GCC可以通过在命令行指定 -std = C99 切换到C99模式。

In newer versions of C language (C99 and later) you can freely mix statements and declarations. GCC can be switched to C99 mode by specifying -std=c99 in command line.

这篇关于ISO C90禁止因为数组的混合声明和code。我该如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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