c语言数组问题
你好 我是 C 语言新手,我有一个问题: 我想通过指针将二维数组发送到函数。 该函数应返回指向二维数组的指针。 我为此编写了以下代码:
#include<stdio.h>
int* put(int *b);
int main()
{
int a[2][3],i,j;
system("clear");
put(a);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\na[%d][%d]= %d",i,j,a[i][j]);
}
}
return 0;
}
int* put(int *b)
{
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=i;
}
}
return b;
}
当我用 gcc2de.c 编译它时,它显示以下错误:
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int *’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:28: error: subscripted value is neither array nor pointer
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
比我只更改以下函数的代码:
#include<stdio.h>
int* put(int **b);
int main()
{
int a[2][3],i,j;
system("clear");
put(a);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\na[%d][%d]= %d",i,j,a[i][j]);
}
}
return 0;
}
int* put(int **b)
{
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=i;
}
}
return b;
}
当我编译它时,我得到以下错误:
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:31: warning: return from incompatible pointer type
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:31: warning: return from incompatible pointer type
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
我做错了什么? 谁能告诉我通过函数指针传递二维数组的方法是什么? 谁能告诉我如何通过函数中的指针返回两个 d 数组
Hi
I'm new to c language i hava a problem :
i want to send a 2-d array to a function via pointer.
The function should return pointer to 2-d array.
I wrote the following code for this :
#include<stdio.h>
int* put(int *b);
int main()
{
int a[2][3],i,j;
system("clear");
put(a);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\na[%d][%d]= %d",i,j,a[i][j]);
}
}
return 0;
}
int* put(int *b)
{
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=i;
}
}
return b;
}
when i compile it with gcc2de.c it shows following errors :
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int *’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:28: error: subscripted value is neither array nor pointer
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
Than i just change the code of function which is following :
#include<stdio.h>
int* put(int **b);
int main()
{
int a[2][3],i,j;
system("clear");
put(a);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("\na[%d][%d]= %d",i,j,a[i][j]);
}
}
return 0;
}
int* put(int **b)
{
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=i;
}
}
return b;
}
when i complie it i got following errors:
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:31: warning: return from incompatible pointer type
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:31: warning: return from incompatible pointer type
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
what I'm doing wrong ?
can anybody tell me what is the way to pass 2d-array via pointers to a function ?
can anybody tell me how to return two d array via pointer in a function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您遇到的第一个错误是您没有传递函数声明的正确类型。因此,要以最少的更正来清理代码,它可能看起来像这样:
两个主要的更正是:
另请注意,由于您传递的是指针,因此您正在修改该地址位置处的值。因此根本不需要返回指针。
希望有帮助。干杯!
The first error that you have is that you are not passing a correct type as declared by your function. So to clean up your code with the least amount of corrections, it would probably look something like this:
The two major corrections are:
Note also that since you're passing in a pointer, you're modifying the value at that address location. Thus there is no need to return a pointer back at all.
Hope it helps. Cheers!
您将 put 的返回值存储在哪里?
根据您的代码,声明应为
int** put( int **)
。where are you storing the return value from
put
?the declaration should be
int** put( int **)
according yo your code.您遇到的第一个错误是您试图在另一个函数中定义一个函数。最简单的做法是在声明的地方定义
put
:第二个问题是,在这两个代码片段中都没有向
put
传递兼容的参数。如果您想将
a
传递给函数,那么您应该注意,作为参数的数组总是衰减为指向其第一个元素的指针。a
的类型为int [2][3]
,即由 2 个 3int
数组组成的数组。这将衰减为指向 3 个int
数组或int (*)[3]
的指针。这应该可以解释您收到的编译错误。您应该将put
声明为:或完全等价的:
因为您无法按值传递数组,所以编译器会自动将采用数组参数的函数声明转换为采用等效指针参数的函数声明。
我已将返回类型更改为
void
,因为您在通过指针传递参数时不需要使用或不需要返回值。您应该从put
的定义中删除return b;
。提示:不要将
int[2][3]
视为二维数组,而是将其视为数组的数组。The first error you have is that you are trying to define a function inside another function. The simplest thing to do is to just define
put
where you declare it:The second problem is that in neither code snippet are you passing a compatible parameter to
put
.If you want to pass
a
to a function then you should note that arrays as arguments always decay to a pointer to their first element.a
has typeint [2][3]
, i.e. an array of 2 arrays of 3int
s. This will decay to a pointer to an array of 3int
s orint (*)[3]
. This should explain the compile error that you are getting. You should declareput
either as:or as the completely equivalent:
Because you cannot pass arrays by value the compiler will automatically convert a function declaration which takes an array parameter to one which takes the equivalent pointer parameter.
I've changed the return type to
void
as you don't use or need the return value as you are passing the parameter by pointer. You should removereturn b;
from your definition ofput
.Hint: Don't think of
int[2][3]
as a 2-d array but as an array of arrays.您不能从 C 中的函数返回二维数组,只能返回指向二维数组第一个元素的指针的一维数组。
也许你会发现这个有用:
Pass 2d array to function in C?
或这样:
C++ 从函数返回多维数组
You can not return a 2d-array from a function in C, you can only return a 1d-array of pointers to the first elements of a 2d-array.
Maybe you could find useful this:
Pass 2d array to function in C?
or this:
C++ Returning multidimension array from function
1.在使用函数之前必须先声明或定义该函数,这与其他流行语言不同。
2.put函数中不需要返回指针,array中的数据已经改变了
3.需要注意类型,int array[][]的类型是int **
1.you should declare or define the function before use it,it's different with other popular luanguage.
2.you do not need to return the pointer in the function put ,the data in array has be changed
3.you needed to notice the type ,the type of int array[][] is int **