我知道二维数组作为一维数组存储在内存中。因此,按照相同的逻辑,我尝试使用单个指针通过引用传递数组,就像对一维数组所做的那样。下面是我的代码:
#include<stdio.h>
void display(int *s)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%d ",s[i][j]);
}
printf("n");
}
}
int main()
{
int s[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
printf("address of the array is %pn",s);
printf("value is %pn",*s);
int i;
printf("address of the repective array isn");
for(i=0;i<3;i++)
{
printf("address of the array is %pn",s[i]);
}
display(s);
return 0;
}
当我尝试编译此消息时,会得到以下消息:
twodarray.c: In function ‘main’:
twodarray.c:25:2: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
display(s);
^
twodarray.c:2:6: note: expected ‘int **’ but argument is of type ‘int (*)[4]’
void display(int *s[3])
^
当我运行上面的代码时,我收到分段错误错误。
函数参数声明为具有类型 int *
void display(int *s)
而作为参数传递给函数的原始数组具有类型
int [3][4]
隐式转换为指向其第一个具有类型的元素的指针
int ( * )[4]
如您所见int *
和int ( * )[4]
是两种不同的类型,并且没有从一种类型到另一种类型的隐式转换。
此外,由于函数参数的类型为 int *
您不能在函数表达式中编写 s[i][j]
. 因为如果将下标运算符应用于此指针,例如s[i]
则此表达式是 int
类型的标量对象。它不是一个指针。因此,您可能不会第二次应用下标运算符。
您必须将参数显式转换为函数调用中的参数类型。例如
display( ( int * )s );
你想要的是以下内容
#include <stdio.h>
void display( int *a, size_t m, size_t n )
{
for ( size_t i = 0; i < m; i++ )
{
for ( size_t j = 0; j < n; j++ )
{
printf( "%2d ", a[i * n + j] );
}
printf( "n" );
}
}
int main( void )
{
int a[3][4] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
printf( "The address of the array is %pn", ( void * )a );
printf( "The address of the first row is %pn", ( void * )*a );
printf("The address of the respective array rows aren");
for ( size_t i = 0; i < 3; i++ )
{
printf( "address of row %zu is %pn", i, ( void * )a[i] );
}
display( ( int * )a, 3, 4 );
return 0;
}
程序输出可能如下所示
The address of the array is 0xbf85d2dc
The address of the first row is 0xbf85d2dc
The address of the respective array rows are
address of row 0 is 0xbf85d2dc
address of row 1 is 0xbf85d2ec
address of row 2 is 0xbf85d2fc
1 2 3 4
5 6 7 8
9 10 11 12
尽管最好通过以下方式声明函数,以避免不必要的强制转换和复杂的函数实现
void display( int ( *a )[4], size_t m );
像您在此处定义的静态二维数组在内存中以顺序一维数组的形式布局。但它不能像你尝试过的那样使用。通常编译器甚至不会生成此代码的二进制文件。
从技术上讲,您可以通过将指针投射到 int*
来调用 display()
函数。这没有多大帮助,因为在函数内部,它是在二维中索引的,编译器不知道维度是什么。
这样想:如果你分配一个 100 整数的线性内存块,这是否意味着它是一个大小为 10x10、2x50 或 4x25 的数组?没有办法知道,所以你不能把它索引为二维数组。此外,甚至可能不知道内存块有多大。
但是,您可以将其索引为一维数组,并将索引手动乘以s[i*4+j]
。这是因为,如前所述,静态数组线性存储在内存中,并且您正在手动告诉如何读取它。
只是想知道您是如何设法实际编译该代码的。
类型 int[3][4]
的数组不可转换为 int**
或 int *[]
或 int*
类型的指针。
问题是,那
int s[3][4];
实际上将存储在物理连续内存中。要访问 3x4 数组的任意部分,函数display
需要知道数组的维度。
因此,您应该将函数更改为:
void display(int (*s)[4])
或使用更灵活的技术(在 C 中将多维数组作为函数参数传递)。
数组按行存储在内存中。因此,首先将存储数组元素 s[0][0],然后存储 s[0][1],s[0][2],s[0][3],s[1][0]...同样。
您已将指向被调用函数中的一维数组的指针作为参数。您所能做的就是更改 printf("%d ",s[i][j]);声明到printf("%d ",*(s + i + j));,这将起作用。
最后的 printf 语句应编辑为 printf("%d ",*(s + 4*i + j));如以下评论所示。