繁体   English   中英

通过函数将值存储在二维数组中

[英]Storing values in a 2D array through function

我编写了以下代码来生成作为用户输入的 3D 坐标点的 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;
}

您正在尝试初始化不在其声明中的数组。 这在 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));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM