c - 错误 : making pointer from integer in C

标签 c pointers compiler-errors int mpi

我正在尝试编写一个 c 程序,它将以并行方式检查随机 vector 是否已排序。

 int isSorted(int size, int array[]) {

 .........
  int *sub_rand_nums = (int *)malloc(sizeof(int) * num_elements_per_proc);

  int orden = isSorted(num_elements_per_proc, sub_rand_nums);

// Gather all partial results to all the processes

  int *sub_ordens = NULL;
  if (my_rank == 0) {
    int  *sub_ordens = (int *)malloc(sizeof(int) * my_size);
  }

if (a == 0) {
    int orden = isSorted(sub_ordens, my_size);
  }




}

有人可以帮助解决我的代码问题吗?它给了我错误

passing argument 1 of ‘isSorted’ makes integer from pointer without a cast [-Wint-conversion]
      orden = isSorted(sub_ordens, my_size);

passing argument 2 of ‘isSorted’ makes pointer from integer without a cast [-Wint-conversion]
      orden = isSorted(sub_ordens, my_size);

最佳答案

您声明了 int isSorted(int size, int array[]) 这表示第一个参数是一个整数。虽然稍后在代码中指定了 int *sub_ordens = NULL; ,它指向一个整数,但它本身并不是一个整数。要修复第一个错误,您需要将整数而不是指向整数的指针传递给 isSorted()。

至于第二个错误,你做了与第一个错误相反的事情。 int array[] 就像说 int *array 但你传递的是 int my_size; 这是一个整数,而不是一个指向整数的指针。

抛开技术方面的问题,让我们将其视为更具体的事物。你有两个人,我们称他们为 A 和 B。A 拿着一张卡片,上面有一个数字,B 指向 A,但是 B 并不知道卡片上写的数字。如果该函数需要一个指针,那么您会寻找 B,但如果它想要数字本身,您会寻找 A。

您的函数要求一个数字(人 A)作为第一个参数,一个指针作为第二个参数(人 B)。

以后如果你的代码在表现上(间距/样式)统一一点,去掉与问题不直接相关的部分,会更容易梳理。

关于c - 错误 : making pointer from integer in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50311460/

相关文章:

c - C和linux中的虚拟地址分配

java - "Non-static method cannot be referenced from a static context"错误

c - C中指针的大小

xcode - 可以将clang静态分析器与返回状态约定混淆吗?

compilation - 包括自定义头文件(ANSI C)

c - 十六进制到字符的转换和转储输出

c++ - 指针行为给出的值为 1

C包含十六进制常量的文件

c - 指针到指针数组中指针的访问地址

c - 为什么++(*ptr) 递增指针?