在 C 中访问二维数组中的元素

发布于 2025-01-19 03:54:11 字数 501 浏览 3 评论 0原文

我正在编写一个程序,该程序将2D char数组传递到用于打印函数中。但是,当我尝试访问2D数组中的元素以进行打印时,我不能。只有在传递数组时,我将打印循环和语句放在Main()函数中时不会发生此问题。

有人可以帮助解释吗?

void disp_array(char* array)
{
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            printf("%c", array[i][j]);
        }
        printf("\n");
    }
}

附件是我的代码:jprintf语句中以错误:

e0142:“表达式必须具有指针到对象类型,但具有类型 int“

I am writing a program that passes a 2D char array into a function for printing. However, when I try to access elements in the 2D array for printing, I can't. This problem does not occur when I place the printing loops and statements in the main() function, only if the array is passed.

Can someone help explain?

void disp_array(char* array)
{
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            printf("%c", array[i][j]);
        }
        printf("\n");
    }
}

Attached is my code: the j in the printf statement is highlighted with an error:

E0142: "expression must have pointer-to-object type but it has type
int"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2025-01-26 03:54:11

函数声明错误。

由于参数的类型为 char *,因此表达式 array[i] 会生成 char 类型的标量对象,该对象将提升为该类型int 由于整数提升,您可能无法在尝试执行 array[i][j] 时应用下标运算符。所以编译器会发出错误消息

E0142:“表达式必须具有指向对象的指针类型,但它具有类型
整数”

声明

void disp_array(char array[][SIZE], int n )
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            printf("%c", array[i][j]);
        }
        printf("\n");
    }
}

该函数应该这样

void disp_array(char (*array)[SIZE], int n )
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            printf("%c", array[i][j]);
        }
        printf("\n");
    }
}

:具有数组类型 char[SIZE][SIZE] 的参数由编译器隐式调整为类型 char ( * )[SIZE]< /code> ,即该函数不知道存在多少 char ( * )[SIZE] 类型的元素,因此您需要显式指定元素的数量。 。

以及您需要传递的数组 数组中的行数例如,

disp_array( array, SIZE );

如果数组包含字符串,则函数定义将如下所示

void disp_array(char (*array)[SIZE], int n )
{
    for (int i = 0; i < n; i++)
    {
        puts( array[i] );
    }
}

The function declaration is wrong.

As the parameter has the type char * then the expressions array[i] yields a scalar object of the type char that is promoted to the type int due to the integer promotions to which you may not apply the subscript operator as you are trying to do array[i][j]. So the compiler issues the error message

E0142: "expression must have pointer-to-object type but it has type
int"

The function should be declared like

void disp_array(char array[][SIZE], int n )
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            printf("%c", array[i][j]);
        }
        printf("\n");
    }
}

or like

void disp_array(char (*array)[SIZE], int n )
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            printf("%c", array[i][j]);
        }
        printf("\n");
    }
}

The parameter having the array type char[SIZE][SIZE] is implicitly adjusted by the compiler to the type char ( * )[SIZE]. That is the function does not know how many elements of the type char ( * )[SIZE] exist. So you need to specify the number of elements explicitly. Try always to define a more general function.

And along with the array you need to pass the number of rows in the array. For example

disp_array( array, SIZE );

If the array contains strings then the function definition will look like

void disp_array(char (*array)[SIZE], int n )
{
    for (int i = 0; i < n; i++)
    {
        puts( array[i] );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文