Reputation: 51
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?
Upvotes: 1
Views: 229
Reputation: 7458
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*)
).
Upvotes: 2
Reputation: 3537
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);
Upvotes: 4
Reputation: 2073
change
p->digits = (int)malloc(sizeof(int) * 1);
to
p->digits = (int*)malloc(sizeof(int) * 1);
Upvotes: 4