有几种方法可以做到这一点。在以下代码中,您可以删除周围的注释
#define NO_PTR_USE 1
在不使用指针的情况下做到这一点。如果使用指针,那么您必须记住,正如@Oli Charlesworth 指出的那样,[] 的优先级高于 *.
/* #define NO_PTR_USE 1 */
#define X 10
#define Y 10
#define ax 1
#define ay 5
struct foo {
int number;
};
typedef struct foo struct_t[X][Y];
void
#if NO_PTR_USE
foo1(struct_t mystruct),
foo2(struct_t mystruct, int x, int y, int n);
#else
foo1(struct_t *mystruct),
foo2(struct_t *mystruct, int x, int y, int n);
#endif
main()
{
struct_t mystruct = {0};
#if NO_PTR_USE
foo1(mystruct);
printf("No pointer was used.\n");
#else
foo1(&mystruct);
printf("Pointer was used.\n");
#endif
printf("mystruct[%d][%d].number = %d\n",
ax, ay, mystruct[ax][ay].number);
}
void
#if NO_PTR_USE
foo1(struct_t mystruct)
#else
foo1(struct_t *mystruct)
#endif
{
foo2(mystruct, ax, ay, 1234);
}
void
#if NO_PTR_USE
foo2(struct_t mystruct, int x, int y, int n)
#else
foo2(struct_t *mystruct, int x, int y, int n)
#endif
{
#if NO_PTR_USE
mystruct[x][y].number = n;
#else
(*mystruct)[x][y].number = n;
#endif
}