gpt4 book ai didi

c - 错误 : making pointer from integer in C

转载 作者:太空宇宙 更新时间:2023-11-04 04:17:22 25 4
gpt4 key购买 nike

我正在尝试编写一个 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/

25 4 0
文章推荐: c - 如何按顺序将元素从一个链表移动到另一个链表
文章推荐: C 源代码、Watcom 编译器和 EMU8086
文章推荐: javascript - 单击以展开
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com