#include <stdio.h>
struct arr_wrapper{
int* arr;
};
struct bar{
struct arr_wrapper* arr_wrapper_obj;
};
void assign_arr(struct arr_wrapper* X){
int arr[3];
for (int i = 0; i < 3; i++){
arr[i] = i;
}
X->arr = arr;
return;
}
void my_func(struct bar* bar_obj){
for (int i = 0; i < 3; i++){
printf("%d\n", bar_obj->arr_wrapper_obj->arr[i]);
}
return;
}
int main()
{
struct arr_wrapper arr_wrapper_obj;
assign_arr(&arr_wrapper_obj);
for (int i = 0; i < 3; i++){
printf("%d\n", arr_wrapper_obj.arr[i]);
}
struct bar bar_obj;
bar_obj.arr_wrapper_obj = &arr_wrapper_obj;
for (int i = 0; i < 3; i++){
printf("%d\n", bar_obj.arr_wrapper_obj->arr[i]);
}
my_func(&bar_obj);
printf("Hello World");
return 0;
}
这行代码的输出为:
0
1
2
0
1
2
32765
1
2
Hello World
这是我的代码的简化版本.
我试图创建一个 struct 的 struct ,内部 struct 有一个array.
然而,数组的大小是动态的,在运行时之前无法确定,所以我使用了一个int指针来指向array.