c - c中数组的问题

标签 c

所以我目前正在开始编程,并且正在尝试编写这段代码,但是我在数组方面遇到了麻烦。我在网上阅读了很多文章,似乎这段代码应该可以工作,但由于某种原因,当我在函数之间传递数组时出现错误。忽略中间注释掉的部分,这是我让它在我的主函数中工作的地方。对于这项任务,我需要它在我下面定义的函数中工作。我只想在一个函数中初始化数组,然后在另一个函数中将其打印出来。 谢谢!

这是它显示的错误 prelab7.c:32:警告:传递“print_array”的参数 1 使指针来自整数而不进行强制转换 prelab7.c:7: 注意:预期为“int *”,但参数的类型为“int”

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int errorcheck(int );
void intializearrary(int[], int);
void print_array(int [], int);

int main()
{
    int maxsize,i;
    int n[maxsize];

    printf("\nEnter the size of the input: ");
    scanf("%d", &maxsize);

    while (errorcheck (maxsize))
    {
        printf("\nInvalid input enter the size of the input again");
        scanf("%d", &maxsize);
    }

/*  srand(time(NULL));

    for (i = 0; i < maxsize; i++)
    {
    n[i] =  generaterandomnumber();
    printf("\nn[%d]=%d", i, n[i]);
    }
*/

    print_array( n[i], maxsize);

return 0;
}

int errorcheck (int maxsize)
{
    if (maxsize < 0 || maxsize > 100)
        return 1;
    else
        return 0;
}

void initializearrary( int n[],int maxsize )
    {
        int i;

        srand(time(NULL));

        for (i = 0; i < maxsize; i++)
        {
            n[i] = rand()%10;
        }
    }

void print_array(int n[], int maxsize)
{
    int i; //counter
    printf("\nInput Array\n");
    for (i = 0; i < maxsize; i++)
    {
{
   printf("\t%d", n[i]);
    }
}

/*int generaterandomnumber()
{
    return rand()%10;
}*/

最佳答案

移动

int n[maxsize];

就在 while 循环和更改之后

print_array( n[i], maxsize);

print_array( n, maxsize);

前者是为了在构建 VLA 之前初始化 maxsize
后者之所以完成,是因为 print_array 需要一个 int* 作为第一个参数,但您传递了一个无效的参数 n[i] 类型int

关于c - c中数组的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29062743/

相关文章:

c - 基于DBusGProxy连接dbus信号失败

c - 在C中设置TCP重传超时

c - 使用 C 在 Linux 上使用 sigsetjmp 的模糊问题

c - 简单便携的 malloc 库

c++ - 使用纯 WinApi 的带有子项的自定义 "container"控件

c - 如何制作动态分配的结构数组?

c - C 中使用 ncursesw 输出 "█"

c - 我用什么代码来询问数量和付款?

c - 在C中动态初始化字符串数组

在 C 中转换静态变量