【问题标题】:Complie error in C program (Visual Studio) regarding Malloc (a value of type "void*" cannot be used to initialize an entity of type "Item*"关于 Malloc 的 C 程序 (Visual Studio) 中的编译错误(“void*”类型的值不能用于初始化“Item*”类型的实体
【发布时间】:2018-05-27 22:16:07
【问题描述】:

在它有malloc(sizeof(Item) 的那一行我得到一个错误:

void* 类型的值不能用于初始化Item* 类型的实体

代码item->color = _color; 给了我一个类似的错误:

const char* 不能分配给 char* 类型的实体

此外,关于 pfirstpsecond 的 compareIdLowHigh 函数 const Item** 给我

const void* 不能用于初始化 const Item** 类型的实体

我不知道如何修复这些错误。

参考代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 7

typedef struct {
    int id;
    double price;
    char* color;
} Item;

Item* initItem(int _id, double _price, const char* _color)
{
    Item* item = malloc(sizeof(Item));
    item->id = _id;
    item->price = _price;
    item->color = _color;
}

void printItem(Item* item)
{
    printf("Id: %d\t\tPrice: %f\t\tColor: %s\n", item->id, item->price,
        item->color);
}

void printItems(Item* items[], size_t size)
{
    for (int i = 0; i < size; ++i)
        printItem(items[i]);
    printf("\n");
}

int compareIdLowHigh(const void* a, const void* b)
{
    const Item** pfirst = a;
    const Item** psecond = b;
    const Item* first = *pfirst;
    const Item* second = *psecond;
    if (first->id < second->id) return -1;
    else if (second->id < first->id) return 1;
    else return 0;
}

int comparePriceHighLow(const void* a, const void* b)
{
    // TODO
}

int compareColorAlpha(const void* a, const void* b)
{
    // TODO
}

int main()
{
    // initialize items
    const size_t COUNT = LENGTH;
    Item* itemPtrs[LENGTH];
    itemPtrs[0] = initItem(0, 12.12, "red");
    itemPtrs[1] = initItem(11, 14.14, "blue");
    itemPtrs[2] = initItem(32, 1.67, "black");
    itemPtrs[3] = initItem(13, 5.54, "brown");
    itemPtrs[4] = initItem(54, 17.20, "purple");
    itemPtrs[5] = initItem(15, 20.24, "yellow");
    itemPtrs[6] = initItem(6, 99.99, "orange");
    printItems(itemPtrs, COUNT);
    // sorting items
    printf("sorting items by id (lowest to highest):\n");
    // TODO
    printItems(itemPtrs, COUNT);
    printf("sorting items by price (highest to lowest):\n");
    // TODO
    printItems(itemPtrs, COUNT);
    printf("sorting items by color (a-z):\n");
    // TODO
    printItems(itemPtrs, COUNT);
    // cleanup items
    for (int i = 0; i < 7; ++i)
        free(itemPtrs[i]);
}

【问题讨论】:

  • malloc 返回一个void*,因此您只需要显式转换回所需的类型:Item* item = (Item*)malloc(sizeof(Item));
  • @monkey0506 仅当您使用 C++ 时。也许你认为你是在用 C 编码,但 Visual Studio 默认是 C++
  • 你是编译成 C++ 还是 C 代码?您的问题标题是 C,这就是我删除 C++ 标记的原因。
  • 你怎么知道你在用 C 编译?尝试在源文件的最顶部临时添加int class = 42;。如果您的编译器对此有所抱怨,则说明它是 C++ 编译器(class 是 C++ 中的关键字,但不是 C 中的关键字)。

标签: c


【解决方案1】:

void* 类型的值不能用于初始化 Item* 类型的实体

malloc 代码很好。您会收到此警告,因为 VS 是不符合标准的编译器,或者因为您将代码编译为 C++。如果将VS设置为C模式编译后问题仍然存在,请获得更好的编译器。

const char* 不能分配给 char* 类型的实体

非常不言自明。要么您允许更改指针,然后在const char* _color 中删除 const 正确性。或者您不允许更改指针,然后将结构成员设为const char* color;

const void* 不能用于初始化 const Item** 类型的实体

const Item** 是指向const Item 的指针。这是与const void* 不同的指针类型,因为它们具有不同的限定符。您可能可以使用诸如const Item*const* pfirst 之类的晦涩内容来解决编译器错误,但这并不是问题的真正根源。

我认为您正在编写一个函数以传递给 bsearch/qsort 等,而您所拥有的是一个指针数组。在这种情况下,您应该只将ab 转换为const Item*。我不知道你为什么首先引入Item**

【讨论】:

    猜你喜欢
    • 2019-02-21
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 2019-08-25
    相关资源
    最近更新 更多