C 中的 void 变量无法编译

发布于 2024-11-27 06:16:32 字数 307 浏览 2 评论 0原文

test.c

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

我使用 gcc 进行编译,但它给了我一个错误:

错误:变量或字段“a”声明为 void

从我在此处读到的内容, 我以为我可以毫无问题地声明 void 变量。

test.c

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

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

error: variable or field 'a' declared void

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄荷梦 2024-12-04 06:16:32

正如您的链接所述:

本身被声明为 void 的变量(如上面的 my_variable)是没有用的;它不能被赋值,不能转换为另一种类型,事实上,不能以任何方式使用。

这意味着虽然语法上正确,但 void 声明没有任何用处,这就是为什么 GCC 可能认为它是一个错误。即使它可以编译,您也无法对变量执行任何操作,所以我猜您的问题仅与测试此行为有关。

事实上,当我们谈论指针 (void*) 时,void 很有用,因为它允许您声明通用指针而不指定类型。

As your link states:

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.

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.

多像笑话 2024-12-04 06:16:32

不,但是您可以声明一个void指针:void *void * 可以保存任何对象指针。

void * 只能保证保存对象(即数据)指针

[但是]

将函数指针转换为 void * 类型是不可移植的

No, but you can declare a void pointer: void *. A void * can hold any object pointer.

void *'s are only guaranteed to hold object (i.e. data) pointers

[but]

it is not portable to convert a function pointer to type void *

傾城如夢未必闌珊 2024-12-04 06:16:32

尽管书中指出 void a; 是一个有效的声明,但我不认为它符合标准。话虽这么说,本书的第一版是 1987 年的,因此它也可能是 GCC 较早实现的延续。

Although the book states that void a; is a valid statement, I don't believe it has ever been standards compliant. That being said, the first edition of the book was from 1987, so it could also be carry-over from older implementations of GCC.

那小子欠揍 2024-12-04 06:16:32

void *x; 是一个有效的语句。

void x; 不是有效的语句。

返回 void 指针的函数也是有效的。

为什么会这样呢?

当在函数中声明变量时,编译器必须为变量分配内存空间。但是当变量的类型为void时,编译器不知道要为该变量分配多少字节。所以这对编译器不起作用。
然而,指向 void 的指针是不同的。指针可以是 void 类型,在读取时可以将其读取为 int、double、float、short 或 char。在这种情况下,需要显式类型转换或自动类型提升由编译器完成。

例如,

int
function_A( void *x )
{
    int *p = (int *)x; 
    return *p;
}

double
function_B( void *x )
{
    double *p = (double *)x; 
    return *p;
}

重要提示:C 不允许直接类型转换 void 指针的取消引用。
我的意思是你不能这样做:

double
function_B( void *x )
{
    return (double)*x; 
}

从概念上讲,这是完全有道理的。但C不允许这样做。

void *x; is a valid statement.

void x; is not a valid statement.

A function that returns a pointer to void is also valid.

Why is this so?

When a variable is declared in a function, the compiler has to allocate a memory space for a variable. But when a variable is of type void, the compiler doesn't know how many bytes to allocate for this variable. So this will not work for the compiler.
However, the pointer to void is different. A pointer can be of type void which can be read as an int or a double or a float or a short or a char at the time of reading. In this case, explicit typecasting required or automatic type promotion is done by the compiler.

e.g.

int
function_A( void *x )
{
    int *p = (int *)x; 
    return *p;
}

double
function_B( void *x )
{
    double *p = (double *)x; 
    return *p;
}

An important note: C does not allow direct typecast of dereference of void pointer.
What I mean by this is that you cannot do this:

double
function_B( void *x )
{
    return (double)*x; 
}

Conceptually, this makes perfect sense. But C doesn't allow this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文