如何将指向结构的指针数组作为参数传递给c

How to pass as parameter an array of pointers to structure in c

我正在尝试将一个指针数组作为参数传递给结构,以在函数中修改它并在 main() 中打印修改后的值。

密码是:

#include "stdio.h"

typedef struct testStruct_s {
   int x;
   int y;
} testStruct;

typedef testStruct typeTab[4];

void modify(typeTab tab)
{
   printf("Before modification %d\n", tab[2].x);
   tab[2].x = 3;
   printf("Modified %d\n", tab[2].x);
}


int main()
{
   typeTab tab[4];
   tab[2]->x = 0;
   printf("First %d\n", tab[2]->x);
   modify(*tab);
   printf("Second %d\n", tab[2]->x);
   return 0;
}

我得到了以下输出:

First 0
Before modification 1719752944
Modify 3
Second 0

我不知道如何在modify()中获取tab[2].x的正确值以及如何修改此值以在tab[2]->x = 3之后打印。

对于我尝试做的事情,需要使用 typedef testStruct

typeTab已经是一个数组,所以typeTab tab[4]声明了一个数组的数组。这意味着 tab[2]->xtab[2][0].x 相同,这不是您想要的。

不要添加额外的维度,然后相应地修改访问权限。

typeTab tab;

tab[2].x = 0;
printf("First %d\n", tab[2].x);
modify(tab);
printf("Second %d\n", tab[2].x);