gpt4 book ai didi

c - 错误, 'invalid operands to binary * (have ' long unsigned int' 和 'int *' )'

转载 作者:太空宇宙 更新时间:2023-11-04 08:02:56 24 4
gpt4 key购买 nike

我在尝试编译我的程序时遇到了问题。

错误:

main.c:26:42: error: invalid operands to binary * (have 'long unsigned int' and 'int *')
array = (int*) malloc(sizeof(int)* size);
^
main.c:30:50: error: invalid operands to binary * (have 'long unsigned int' and 'int *')
array = (int*) realloc(array, sizeof(int)* size) ;

相关代码如下:

int* arraySize(int* array, int* size)
{
size = 0;
while ((size > 20) || (size < 1))
{
printf("What is the size of your array? (1 - 20)\n");
scanf("%d", &size);
if ((size > 20) || (size < 1))
{
printf("Invalid selection.\n");
}
}
//checking if array has been allocated before
if(array = NULL)
{
array = (int*) malloc(sizeof(int)* size);
}
else
{
array = (int*) realloc(array, sizeof(int)* size) ;
}
printf("\nSize of array: array[%d]\nReturning...", size);
return array;
}

谁能给我解释一下这是怎么回事?

TIA

最佳答案

您的参数大小太多了。这使得 size 成为 int 的指针。这又需要取消引用(*大小而不是简单的大小)以使用值指针指向。但我认为这是一个错误,你不想让 size 成为一个指针。

更改为:

int* arraySize(int* array, int size)

关于c - 错误, 'invalid operands to binary * (have ' long unsigned int' 和 'int *' )',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44761462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com