c - 如何用 C 中的数组解决这个问题?

标签 c arrays

我将一个数组传递给一个函数,该函数的大小由用户自己确定。

  #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/

相关文章:

java - 使用二分查找查找数组中的元素

c - 如何在C语言的cooja模拟器中获得一个随时间变化的随机数?

c - VS中C中可变大小的数组

c - 将 3d 数组发送到 CUDA 内核

PHP:使用另一个未排序的对象数组更新对象数组值

javascript - 创建按日期分组并具有 count 属性的对象数组

php - 替换多维php数组中的值

c - 是否有一种内置信号量结构允许等待多个资源?

c - 为什么 MATLAB 使用列优先顺序?

javascript - 合并/替换 Javascript 数组