2016-09-12 93 views
-2

我編寫了下面的代碼,以生成用作用戶輸入的三維座標點的26個鄰域體素。我使用了一個函數nbr26,它生成相鄰體素的座標值,但得到下面的錯誤和警告。通過函數將值存儲在二維數組中

plane.c: In function ‘nbr26’: 
plane.c:28:18: error: **expected expression before ‘{’ token** 

Arr[26][3] = {{p0-1,q0,r0},{p0+1,q0,r0},{p0,q0-1,r0},{p0,q0+1,r0},{p0,q0,r0-1},{p0,q0,r0+1},{p0-1,q0-1,r0},{p0+1,q0-1,r0},{p0-1,q0+1,r0},{p0+1,q0+1,r0},{p0-1,q0,r0-1},{p0+1,q0,r0-1},{p0-1,q0,r0+1},{p0+1,q0,r0+1},{p0,q0-1,r0-1},{p0,q0-1,r0+1},{p0,q0+1,r0-1},{p0,q0+1,r0+1},{p0-1,q0-1,r0-1},{p0-1,q0-1,r0+1},{p0-1,q0+1,r0-1},{p0-1,q0+1,r0+1},{p0+1,q0-1,r0-1},{p0+1,q0-1,r0+1},{p0+1,q0+1,r0-1},{p0+1,q0+1,r0+1}}; 
      ^

plane.c:29:5: warning: **return from incompatible pointer type** [enabled by default] 
return Arr; 
^ 

請看看,並在問題上的一些燈光!

#include<stdio.h> 
#include<stdlib.h> 

int** nbr26(int, int, int, int[][3]); 
int main() 
{ 
    int x0, y0, z0, a, b, c, d, Ar[26][3]; 

    printf("\nPlease enter a point on the plane : "); 
    scanf("%d %d %d", &x0, &y0, &z0); 

    printf("\nPlease enter a norm of the plane : "); 
    scanf("%d %d %d", &a, &b, &c); 

    printf("\nUser Input"); 
    printf("\n(%d,%d,%d)", x0, y0, z0); 
    printf("\t<%d,%d,%d>", a, b, c); 
    d = -a*x0-b*y0-c*z0; 
    printf("\nScaler Equation of the given plane is : %dx+%dy+%dz+ (%d)=0", a, b, c, d); 

    int** ra = nbr26(x0,y0,z0,Ar); 
    printf("\n26 neighbours of the given point are : "); 
    printf("(%d,%d,%d)\n",x0-1,y0-1,z0-1); 
    return 0; 
} 
int** nbr26(int p0, int q0, int r0, int Arr[26][3]) 
{ 
    Arr[26][3] = {{p0-1,q0,r0},{p0+1,q0,r0},{p0,q0-1,r0},{p0,q0+1,r0}, {p0,q0,r0-1},{p0,q0,r0+1},{p0-1,q0-1,r0},{p0+1,q0-1,r0},{p0-1,q0+1,r0},{p0+1,q0+1,r0},{p0-1,q0,r0-1},{p0+1,q0,r0-1},{p0-1,q0,r0+1},{p0+1,q0,r0+1},{p0,q0-1,r0-1},{p0,q0-1,r0+1},{p0,q0+1,r0-1},{p0,q0+1,r0+1},{p0-1,q0-1,r0-1},{p0-1,q0-1,r0+1},{p0-1,q0+1,r0-1},{p0-1,q0+1,r0+1},{p0+1,q0-1,r0-1},{p0+1,q0-1,r0+1},{p0+1,q0+1,r0-1},{p0+1,q0+1,r0+1}}; 
    return Arr; 
} 
+1

你不能做到這一點。只有當你聲明它時,你才能以這種方式初始化數組。您可以使用[memcpy](http://www.tutorialspoint.com/c_standard_library/c_function_memcpy.htm)或其他工藝來完成這項工作, – LPs

+2

...和2D數組,它不是指針的指針。 – LPs

回答

-1

您正嘗試初始化不在其聲明中的數組。這在C或C++中不合法。您只能將其與聲明一起初始化。

你可以做的唯一的事情就是直接設置值或從另一個數組拷貝。

所以基本上你是被迫做任何

void nbr26(int p0, int q0, int r0, int Arr[26][3]) 
{ 
    Arr[0][0] = p0 - 1; 
    Arr[0][1] = q0; 
    ... 
} 

要麼

void nbr26(int p0, int q0, int r0, int Arr[26][3]) 
{ 
    const int buffer[26][3] = {{p0-1,q0,r0}, ... }; 
    memcpy(Arr, buffer, 26*3*sizeof(int)); 
} 
+1

像int [1] [2]這樣的東西衰變成int(*)[2]',從來沒有'int *'! N維數組與1D數組不同。 – Olaf