Reputation: 73
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**
}
Upvotes: 2
Views: 9097
Reputation: 778
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.
Upvotes: 1
Reputation: 244
I believe that:
AddCost(console);
is returning an integer which is then casted to a pointer (what the warning said).
Upvotes: 1
Reputation: 7673
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 ?
Upvotes: 0
Reputation: 17595
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*
Upvotes: 1