不能在C无效变量编译 [英] Can't compile with void variable in C

查看:158
本文介绍了不能在C无效变量编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

test.c以

int main () {
   void a;
   return 0;
}

我用gcc来编译,但它给了我一个错误:

I use gcc to compile but it gives me an error:

错误:变量或字段'A'宣告无效

error: variable or field 'a' declared void

从我读这里
我以为我可以声明作废变量没有问题。

From what I read here, I thought I can declare void variable without problem.

推荐答案

由于你的链接状态:

这本身就是宣布无效(如my_variable以上)变量是没用的;它不能被分配​​的值,不能转换为另一种类型,事实上,不能以任何方式使用。

A variable that is itself declared void (such as my_variable above) is useless; it cannot be assigned a value, cannot be cast to another type, in fact, cannot be used in any way.

这意味着,虽然语法正确,一个无效的声明是没有任何有用的东西,这就是为什么GCC会认为这是一个错误。即使它会编译,你将无法与变量做任何事情,所以我想你的问题只是涉及到测试这一行为。

This means that although syntactically correct, a void declaration is not useful to anything, this is why GCC could consider it an error. Even if it would compile, you won't be able to do anything with the variable, so I guess your question is just related to testing this behavior.

在事实上无效只是,当我们谈论的指针有用的(无效* ),因为它可以让你声明一个通用指针不指定类型。

In fact void is useful just when we're talking about pointers (void*) since it allows you to declare a generic pointer without specifying the type.

这篇关于不能在C无效变量编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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