2

我收到警告:初始化从整数生成指针,而没有在 C 中进行强制转换。什么是强制转换?我应该怎么办?

void UpdateElement(Console* console)
{
    DynamicVector* CostList=getAllCosts(console->ctrl);
    int i,n;
    printf("Give the position of the element you want to delete:");
    scanf("%d",&n);
    for(i=n-1;i<getLen(CostList);i++)
    {
        Cost* c=(Cost*)getElementAtPosition(CostList,i);
        Cost* c2=AddCost(console); **//here I get the warning**
        update_an_element(console->ctrl,c,c2,i);
    }
}

Console* initConsole(Controller* ctrl)
{
    Console* console=(Console*)malloc(sizeof(Console));
    console->ctrl=ctrl;
    return console;
}

int createCost(Controller* ctrl, char* day, char* type, int sum)
{
    Cost* c=initCost(day,type,sum);
    save(ctrl->repo,c);
    return c; **//now here I get the warning**

}
4

4 回答 4

1

C/C++ 假定返回类型是整数,除非由标头或其声明指定。您可能调用了一个未在程序中预先声明且没有标头的函数。它假设它是一个 int 并给你一个错误。

于 2013-12-08T23:31:16.907 回答
1

我相信:

AddCost(console);

正在返回一个整数,然后将其转换为指针(警告所说的)。

于 2013-03-17T18:59:49.903 回答
1

c是 typeCost*并且函数createCost返回int. 两者都不兼容,这就是编译器抱怨缺少强制转换的原因,但在这种情况下你不想强制转换。

将该函数的返回类型更改为Cost*

于 2013-03-17T19:00:44.863 回答
0

您可能需要使用

Cost* c2=(Cost*)AddCost(console);

但它可能是不安全的,因为 AddCost(...) 正在返回另一种类型。

至于功能

int createCost(Controller* ctrl, char* day, char* type, int sum)

它应该被声明为

Cost* createCost(Controller* ctrl, char* day, char* type, int sum)

为什么它被声明为 int ?

于 2013-03-17T18:56:16.617 回答