C语言 从“void*”到“node*”的无效转换[-fpermissive]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16793587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:29:49  来源:igfitidea点击:

invalid conversion from 'void*' to 'node*' [-fpermissive]

c

提问by STAR S

I have a C program that produces an error:

我有一个产生错误的 C 程序:

invalid conversion from 'void*' to 'node*' [-fpermissive]

Here's my code:

这是我的代码:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node* next;
};

struct node* onetwothree();

int main()
{
    struct node* ptr;
    ptr = onetwothree();
    return 0;
}

struct node* onetwothree()
{
    struct node* head;
    struct node* temp;
    head = malloc(sizeof(struct node));
    temp = head;
    for(int i=1; i<=3; i++)
    {
        temp->data = i;
        if(i<3)
            temp=temp->next;
        else
            temp->next = NULL;
    }
    return head;
}

What am I doing wrong?

我究竟做错了什么?

回答by hmjd

In C, a void*is implicity convertible to a T*where Tis any type. From section 6.3.2.3 Pointersof the C99 standard:

在 C 中, avoid*可以隐式转换为 a T*whereT是任何类型。来自C99 标准的第6.3.2.3指针

A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

指向 void 的指针可以与指向任何不完整或对象类型的指针相互转换。指向任何不完整或对象类型的指针可以转换为指向 void 的指针,然后再返回;结果应与原始指针相等。

malloc()returns a void*and is assignable without casting to head, a struct node*. This is not true in C++, so I suspect a C++ compiler is being used to compile this C code.

malloc()返回 avoid*并且无需强制转换为head, a即可赋值struct node*。这在 C++ 中并非如此,因此我怀疑正在使用 C++ 编译器来编译此 C 代码。

For example:

例如:

#include <stdlib.h>

int main()
{
    int* i = malloc(sizeof(*i));
    return 0;
}

when compiled with:

编译时:

gcc -Wall -Werror -pedantic -std=c99 -pthread main.c -o main

gcc -Wall -Werror -pedantic -std=c99 -pthread main.c -o main

emits no errors. When compiled with:

不发出错误。编译时:

g++ -Wall -Werror -pedantic -std=c++11 -pthread main.cpp -o main

g++ -Wall -Werror -pedantic -std=c++11 -pthread main.cpp -o main

emits:

发出:

main.cpp: In function 'int main()': main.cpp:5:31: error: invalid conversion from 'void*' to 'int*' [-fpermissive]

main.cpp:在函数“int main()”中:main.cpp:5:31:错误:从“void*”到“int*”的无效转换[-fpermissive]



Additionally, the onetwothree()function is not allocating memory correctly. It allocates one struct nodeonly:

此外,该onetwothree()函数未正确分配内存。它只分配一个struct node

head = malloc(sizeof(struct node));

and then, eventually, dereferences head->next->nextwhich is undefined behaviour. An individual malloc()is required for every struct node. Remember to free()what was malloc()d.

然后,最终,取消引用head->next->next这是未定义的行为。一个人malloc()需要每一个struct node。记住free()什么是malloc()d。

回答by Alex Erny

You're having this warning/error because you are using malloc(which returns a void*)to initialize a structure of type node*without doing an explicit cast.

您收到此警告/错误是因为您正在使用malloc(返回 a void*)来初始化类型结构node*而不进行显式转换。

To get rid of this error you could change your code this way :

要摆脱此错误,您可以通过以下方式更改代码:

head = (struct node *)malloc(sizeof(struct node));

or you could as well add the "-fpermissive" flag to your compiler which will then ignore these errors.

或者您也可以将“-fpermissive”标志添加到您的编译器,然后将忽略这些错误。

EDIT: But yeah, i didn't think about the fact that this should not happen in a C compiler anyways

编辑:但是,是的,我没有想过这不应该发生在 C 编译器中的事实