c - 警告 :"initialization makes pointer from integer without a cast in c

标签 c compiler-errors

    for( k = 0; k < CycleCount;k++)
    {
        //make Org data
        int* Data = MakeData(DataCount[i]);
  ......

这个函数看起来像这样。 我认为这是对的。所以……

  int* MakeData(int DataCount)
  {
        //
   int* Data=(int*)malloc(DataCount*sizeof(int));
   int i;
   for( i=0; i<DataCount; i++)
   {
      //
      Data[i] = rand()%DataCount +1;
   }
   return Data;
 }

我不知道为什么这不起作用。

我该怎么办???

最佳答案

当 C 编译器在没有看到函数原型(prototype)的情况下发现一个函数调用时,它假定一个返回 int 的函数。

您应该使用“函数原型(prototype)”告诉编译器正确的函数签名:

int* MakeData(int DataCount);

这应该放在一个 .h 文件中,该文件将包含在调用或定义该函数的所有编译单元中。

如果您有一个仅在当前编译单元中可见的静态函数,您可以将原型(prototype)(包括 static )放在该文件中所有函数之前。

此外,你不应该从 malloc 返回.它返回 void* .在 C 语言中,这可以转换为任何其他指针类型。当您 #include <stdlib.h> 时,您会得到正确的原型(prototype).

关于c - 警告 :"initialization makes pointer from integer without a cast in c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22963595/

相关文章:

haskell - 为什么Haskell类型系统无法捕获这一点?

c++ - 使用 map<string,struct> 类型的映射编译头文件时出错

c - 如果字符串的第一部分为 0,则 strtok() 忽略 0 值

c - 使用哪种加密方法?

c - 为什么我得到两个不同的结果? ( boolean 值问题)

java - maven "cannot find symbol"消息无用

c - 在 C 中,实现头文件的最低要求是什么?

c - C 中 ' # ' 字符的右对齐

c++ - 错误C2664 'BOOL CryptBinaryToStringW(const BYTE *,DWORD,DWORD,LPWSTR,DWORD *)' : cannot convert argument 4 from 'std::unique_ptr' to 'LPWSTR'

iphone - 访问未知的setter方法