我将一个数组传递给一个函数,该函数的大小由用户自己确定。
#include<stdio.h>
void mapper(int a[], int size);
int main ()
{
int n;
printf("Please provide the number of inputs\n");
scanf("%d",&n);
int array[n];
for(int i=0; i<n;i++)
{
scanf("%d",&array[i]);
}
mapper(array,n);
return 0;
}
void mapper (int a[], int size)
{
for (int i=0; i<size;i++)
{
for(int j=0; j<sizeof(a[i]);j++)
{
printf("#");
}
printf("\n");
}
}
当我运行这个程序时,我收到一个错误,称为有符号整数与无符号整数的比较。为什么会这样呢?是因为我传递了 a[] 作为参数,后来又使用了 a[i] 吗?
如何将数组作为参数传递给要确定大小的函数?
最佳答案
When I run this program I get an error called the comparison of signed integer to unsigned integer. Why is it so?
您收到该错误是因为
for(int j=0; j<sizeof(a[i]);j++)
sizeof
生成 size_t
类型的值,该值是无符号类型,您可以将其与 int
进行比较。
关于c - 如何用 C 中的数组解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54266167/