错误C2440初始化无法将void转换为typ [英] Error C2440 initializing cannot convert void to typ

查看:56
本文介绍了错误C2440初始化无法将void转换为typ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdlib.h>
#include <stdio.h>
// self - referential structure
struct listNode {
char data; // each listNode contains a character
struct listNode *nextPtr; //pointer to next node
};

typedef struct listNode ListNode;  // synonym for struct listNode
typedef ListNode *ListNodePtr; // synonym for struct listnode*



void insert(ListNodePtr *sPtr, char value)
{
ListNodePtr newPtr = malloc(sizeof(ListNode)); // create node

if (newPtr != NULL) // is space availale?
{
    newPtr->data = value; // place value in node
    newPtr->nextPtr = NULL; // node does not link to another node

     ListNodePtr previousPtr = NULL;
    ListNodePtr currentPtr = *sPtr;

    // loop to find the correct location in the list
     while (currentPtr != NULL && value > currentPtr->data)
     {
        previousPtr = currentPtr; // walk to...
         currentPtr = currentPtr->nextPtr; // ... next node
     }

    if (previousPtr == NULL) // insert new node at beginning of list
     {
        newPtr->nextPtr = *sPtr;
        *sPtr = newPtr;
    }
    else
    {
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
    }
}

else 
{
    printf("%c not inserted. No memory available. \n", value);
}
}

这是我的功能,问题在线发生 "ListNodePtr newPtr = malloc(sizeof(ListNode));" 它说Cannoy将void转换为ListNotePtr 然后说 IntelliSense:无法使用类型为"void *"的值来初始化类型为"ListNodePtr"的实体

this is my function, the problem happens in line "ListNodePtr newPtr = malloc(sizeof(ListNode));" It says Cannoy convert void to ListNotePtr Then it says IntelliSense: A value of type "void *" cannoy be used to initialize entity of type "ListNodePtr"

我有点困惑,因为我直接从演讲幻灯片中复制了此功能,但是我似乎无法使其正常工作.有人知道发生了什么吗?此函数和其他函数在main(void)中调用. 预先谢谢你!

I'm a bit confused because I copied this function straight from my lecture slides, but I cannot seem to make it work. Anybody know what's going on? This function and others are called in main(void). Thank you in advance!

推荐答案

似乎是您正在将空类型值分配给ListNodePtr变量. 如果使用C ++编译器进行编译,则需要执行显式转换:

It seems to be that you are assigning a void type value to a ListNodePtr variable. If you are compiling with a C++ compiler, you need to perform an explicit cast:

void insert(ListNodePtr *sPtr, char value)
{
    ListNodePtr newPtr = (ListNodePtr)malloc(sizeof(ListNode));
// ...

这里有一个类似的问题: https://stackoverflow.com/a/15106036/5810934 .

There is a similar issue here : https://stackoverflow.com/a/15106036/5810934.

这篇关于错误C2440初始化无法将void转换为typ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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