c语言数组问题

问题描述:

嗨 我是新来的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; 
} 

当我complie它,我得到了以下错误:

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 

我做错了什么? 任何人都可以告诉我什么是通过指针传递2d数组的方式? 任何人都可以告诉我如何通过函数中的指针返回两个d数组

+0

在'printf(“\ na [%d] [%d] =%d”,i,j,a [i] [j])之后会出现'}';',防止编译。 – mbq 2011-05-02 07:53:12

,你必须是你是不是传递一个正确的类型由所申报的第一个错误你的功能。因此,要清理与修正量最少的代码,它可能会是这个样子:

#include<stdio.h> 

void put(int *b); 

int main() 
{ 
    int a[2][3],i,j; 

    put(&a[0][0]); 

    for(i=0;i<2;i++) 
    { 
    for(j=0;j<3;j++) 
    {  
     printf("\na[%d][%d]= %d", i, j, a[i][j]); 
    } 
    } 

    printf("\n\n"); 

    system("PAUSE"); // Not recommended, but works for now 
return 0; 
} 

void put(int *b) 
{ 
    int count = 1; 
    int i, j; 

    for(i=0;i<2;i++) 
    { 
    for(j=0;j<3;j++) 
    { 
     //b[i][j]=i; 
     *(b + ((i*3) + j)) = count++; 
    } 
    } 

} 

两个主要的修正是:

  1. 您通过您的2-起始地址D数组明确地将其作为& a [0] [0]。
  2. 另外,请注意当您使用int * b时也必须使用的指针算法。

请注意,由于您传递的是指针,因此您正在修改该地址位置处的值。因此不需要返回指针。

希望它有帮助。干杯!

你在哪里存储put的返回值?

声明应该是int** put(int **)根据你的代码。

您遇到的第一个错误是您正试图在另一个函数中定义一个函数。做最简单的事情是只定义put在那里你将它声明:

int put() 
{ 
    /* definition of put */ 
} 

int main() 
{ 
    /* body calls put */ 
} 

的第二个问题是,在没有代码片段,你传递一个兼容的参数put

如果您想将a传递给函数,那么您应该注意,数组作为参数总是衰减为指向其第一个元素的指针。

a具有类型int [2][3],即具有3个int的2个阵列的阵列。这会衰减到指向数组3 intint (*)[3]的指针。这应该解释你所得到的编译错误。你应该申报put无论是作为:

void put(int (*b)[3]); 

或完全等同:

void put(int b[][3]); 

因为你不能按值传递数组,编译器会自动转换函数声明它接受一个数组参数其中之一采用等效的指针参数。

我已经将返回类型更改为void,因为您在使用指针传递参数时未使用或需要返回值。您应该从put的定义中删除return b;

提示:不要把int[2][3]当作一个二维数组,而是作为一个数组的数组。

您不能从C中的函数返回2d数组,您只能返回指向2d数组的第一个元素的1d数组指针。

也许你可以找到有用的:

Pass 2d array to function in C?

或本:

C++ Returning multidimension array from function

1.您应该在使用前声明或定义该功能,它与其他流行的语言不同。

2.you不需要在函数返回放指针,在阵列中的数据已被改变

3.you [] []是INT,以通知的类型,int数组的类型所需**