1

I am compiling some code for a program that memoizes fibonacci numbers. The program works perfectly however when I compile in linux environment I get this warning before compiling.

line 61: warning: assignment makes pointer from integer without a cast [enabled by default]

Here is a snippet of code of where this warning is coming from, I'll try to show things of most relevance to get the best picture presented of the situation

int InitializeFunction(intStruct *p, int n)

    p->digits = (int)malloc(sizeof(int) * 1);

        if(p->digits == NULL)
            handleError("Got error");

    //making the one index in p->digits equal to n
    p->digits[0] = n;

    //incrementing a field of 'p'
    p->length++;

    //return 1 if successful
    return 1;

This function is only called twice for a very specific purpose. it is used to initialize the two base cases in the fibonacci sequence f[0] = 0 & f[1] = 1. That's it's only purpose. So if I pass by reference the address of a specific index in an array of structs it's just supposed to initialize those values:

Initializer(&someIntStruct[0], 0) ---->  after function -> someIntStruct[0] == 0
Initializer(&someIntStruct[1], 1) ---->  after function -> someIntStruct[1] == 1

thoughts?

4

3 回答 3

4

change

p->digits = (int)malloc(sizeof(int) * 1);

to

p->digits = (int*)malloc(sizeof(int) * 1);
于 2013-06-19T02:42:20.050 回答
4

I suspect the warning has to do with this line:

p->digits = (int)malloc(sizeof(int) * 1);

Since malloc returns a void* , whereas you are casting it to int. I suppose p->digits is an int* ?

Try changing it to:

p->digits = (int *)malloc(sizeof(int) * 1);
于 2013-06-19T02:42:39.577 回答
2

It's considered bad practice to cast the result of malloc.

And as already pointed out you're not doing it correctly either (should be (int*) ).

于 2013-06-19T02:45:21.543 回答