3

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**

}
5
  • 1
    Please show us the declaration of Console. Commented Mar 17, 2013 at 18:55
  • Are there prototypes for all functions used within UpdateElement()? Or are they all defined beforehand? If not then an implicit declaration will be generated, that returns an int. Commented Mar 17, 2013 at 18:56
  • 1
    YOu don't need to cast the return value of malloc in a C program. Commented Mar 17, 2013 at 18:56
  • 1
    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. Commented Mar 17, 2013 at 18:58
  • 2
    @CucerzanRares: Maybe you should first write a question with all needed information instead of editing it every minute. Commented Mar 17, 2013 at 19:03

4 Answers 4

1

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*

Sign up to request clarification or add additional context in comments.

Comments

1

I believe that:

AddCost(console);

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

2 Comments

AddCost returns because all the functions were void and I couldn't do Cost* c2-function() so I made AddCosts return.
When AddCost() returns an integer then you know where and why there is a warning.
1

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.

Comments

0

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 ?

5 Comments

It worked there. Now I am getting the warning in controller. I edited the post with the code.
@CucerzanRares Then you need return (int)c; but your program will probably crash. The function should probably be declared as Cost* createCost(…).
In which way is the call of AddCost(console) connected to the function createCost()?
@flyingOwl It is not really connected. There are two distinct warnings. One because the return type of createCost(…) is int where, apparently, it should be Cost*. One because it seems that the return type of AddCost(…) is int and the OP assigns (cast) it to a pointer of type Cost*. The only connection between the two is that the both involve an improper conversion from int to Cost* and vice versa.
You're right, the OP has mixed up a lot casts and return types. I just wanted to add what I said above :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.