赋值使得指针来自int w / out of cast

我正在为一个记忆斐波那契数字的程序编译一些代码。 该程序工作正常但是当我在linux环境中编译时,我在编译之前得到了这个警告。

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

以下是此警告来自的代码片段,我将尝试展示最相关的内容,以便了解最佳情况

 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; 

此function仅针对特定目的调用两次。 它用于初始化斐波那契序列f [0] = 0&f [1] = 1中的两个基本情况。这是唯一的目的。 因此,如果我通过引用传递结构数组中特定索引的地址,那么它应该初始化这些值:

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

想法?

更改

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

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

我怀疑警告与此行有关:

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

因为malloc返回void *,而你将它转换为int。 我想p->digits是一个int*

尝试将其更改为:

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

转换malloc的结果被认为是不好的做法。

正如已经指出的那样,你也没有正确地做到(应该是(int*) )。