2011-05-02 112 views
0

嗨 我是新來的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

回答

0

,你必須是你是不是傳遞一個正確的類型由所申報的第一個錯誤你的功能。因此,要清理與修正量最少的代碼,它可能會是這個樣子:

#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時也必須使用的指針算法。

請注意,由於您傳遞的是指針,因此您正在修改該地址位置處的值。因此不需要返回指針。

希望它有幫助。乾杯!

0

你在哪裏存儲put的返回值?

聲明應該是int** put(int **)根據你的代碼。

0

您遇到的第一個錯誤是您正試圖在另一個函數中定義一個函數。做最簡單的事情是隻定義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]當作一個二維數組,而是作爲一個數組的數組。

0

1.您應該在使用前聲明或定義該功能,它與其他流行的語言不同。

2.you不需要在函數返回放指針,在陣列中的數據已被改變

3.you [] []是INT,以通知的類型,int數組的類型所需**