【发布时间】:2019-04-22 02:07:33
【问题描述】:
我正在尝试将指向结构的指针数组作为参数传递,在函数中对其进行修改并在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。
【问题讨论】:
-
您的代码中没有“指向结构的指针数组”。
标签: c arrays pointers structure