将2D数组传递给带指针的函数

时间:2021-12-28 21:42:07
#include <stdio.h>

void func(int **);
int main(void)
{
    int ptr[2][3] = { {8, 7, 3}, {4, 5, 6} };
    printf("ptr point is %p\n", ptr);
    printf("*ptr point is %p\n", *ptr);
    func(ptr);

    system("pause");
    return 0;
}

void func(int *ptr[8])
{
    printf("ptr point is %p\n", ptr);
    printf("*ptr point is %p\n", *ptr);
    printf("*(ptr + 1) point is %p\n", *(ptr + 1));
}

Output:

ptr point is 004FFC24
*ptr point is 004FFC24
ptr point is 004FFC24
*ptr point is 00000008
*(ptr + 1) point is 00000007
Press any key to continue

Why ptr now becomes a 1D pointer?

为什么ptr现在成为一维指针?

As you can see *(ptr + 1) is the same value in main: the 8 and 7 in {{8, 7, 3}, {4, 5, 6}}.

如你所见*(ptr + 1)在main中是相同的值:{{8,7,3},{4,5,6}}中的8和7。

1 个解决方案

#1


1  

You have several issues.

你有几个问题。

First, you declare func to take a char **, but later define it to take int *[8], which doesn’t match. Even worse, both are wrong - or rather, they don’t match the type of the argument you’re passing.

首先,声明func以获取char **,但稍后将其定义为采用不匹配的int * [8]。更糟糕的是,两者都是错误的 - 或者更确切地说,它们与您传递的参数的类型不匹配。

Except when it is the operand of the sizeof or unary & operators, an expression of type “N-element of T” will be converted (“decay”) to an expression of type “pointer to T”, and the value of the expression will be the address of the first element.

除非它是sizeof或一元&运算符的操作数,否则“T的N元素”类型的表达式将被转换(“衰减”)为“指向T的指针”类型的表达式,并且表达式的值将是第一个元素的地址。

When you pass ptr to func, it is converted from type “2-element array of 3-element of int” to “pointer to 3-element of int”, or int (*)[3]. So the function prototype needs to be

当你将ptr传递给func时,它会从类型为“3元素的int元素的2元素数组”转换为“指向3元素的int”或int(*)[3]。所以函数原型需要

void func( int (*ptr)[3] )

or

void func( int ptr[][3] )

Because your function definition and declarations don’t match, and because neither matches the actual type of the argument, you get the unexpected output. You should have gotten a warning on the mismatched function argument types; if not, you may need to raise the warning level.

因为函数定义和声明不匹配,并且因为它们都不匹配参数的实际类型,所以会得到意外的输出。你应该对不匹配的函数参数类型发出警告;如果没有,您可能需要提高警告级别。

#1


1  

You have several issues.

你有几个问题。

First, you declare func to take a char **, but later define it to take int *[8], which doesn’t match. Even worse, both are wrong - or rather, they don’t match the type of the argument you’re passing.

首先,声明func以获取char **,但稍后将其定义为采用不匹配的int * [8]。更糟糕的是,两者都是错误的 - 或者更确切地说,它们与您传递的参数的类型不匹配。

Except when it is the operand of the sizeof or unary & operators, an expression of type “N-element of T” will be converted (“decay”) to an expression of type “pointer to T”, and the value of the expression will be the address of the first element.

除非它是sizeof或一元&运算符的操作数,否则“T的N元素”类型的表达式将被转换(“衰减”)为“指向T的指针”类型的表达式,并且表达式的值将是第一个元素的地址。

When you pass ptr to func, it is converted from type “2-element array of 3-element of int” to “pointer to 3-element of int”, or int (*)[3]. So the function prototype needs to be

当你将ptr传递给func时,它会从类型为“3元素的int元素的2元素数组”转换为“指向3元素的int”或int(*)[3]。所以函数原型需要

void func( int (*ptr)[3] )

or

void func( int ptr[][3] )

Because your function definition and declarations don’t match, and because neither matches the actual type of the argument, you get the unexpected output. You should have gotten a warning on the mismatched function argument types; if not, you may need to raise the warning level.

因为函数定义和声明不匹配,并且因为它们都不匹配参数的实际类型,所以会得到意外的输出。你应该对不匹配的函数参数类型发出警告;如果没有,您可能需要提高警告级别。