2
votes

I am getting the warning: initialization makes pointer from integer without a cast in C. What is a cast? What should I do?

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
Please show us the declaration of Console. - flyingOwl
@flyingOwl I edited the post. - Cucerzan Rares
YOu don't need to cast the return value of malloc in a C program. - Carl Norum
Could you post the declaration of the AddCost(…) function ? Seems it returns an int. You make it a pointer. That could be bad. See my answer. - Jean
@CucerzanRares: Maybe you should first write a question with all needed information instead of editing it every minute. - flyingOwl

4 Answers

1
votes

c is of type Cost* and the function createCost returns int. both are not compatible that's why the compiler complains about a missing cast, but you don't want to cast in this case.

Change the return type of that function to Cost*

1
votes

I believe that:

AddCost(console);

is returning an integer which is then casted to a pointer (what the warning said).

1
votes

C/C++ assumes the return type is an integer unless specified by a header or it's declaration. You probably called a function that wasn't declared beforehand in the program and didn't have a header. It assumed it was an int and gave you an error.

0
votes

You may need to use

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

But it may be unsafe since AddCost(...) is returning an other type.

As for the function

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

It should be declared as

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

Why is it declared as int ?