を使用する場合は、 ではなくinnerStruct
として宣言します。inner *
struct inner
malloc()
宣言したようにinner
、outer
.
を使用したので、その型の変数を宣言するときにキーワードtypedef
は必要ないことにも注意してください。struct
コンパイルして実行するコードの修正版を次に示します。
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char address[32]; // 32 chars are allocated when an inner is created
} inner;
typedef struct {
inner innerStruct; // innerStruct is allocated when an outer is created
} outer;
typedef struct {
inner *innerStruct; // innerStruct must be allocated explicitly
} outer2;
int main(int argc, char *argv[]) {
int i = 0;
outer *outerArray;
outer2 *outer2Array;
outer *outerReference;
outer2 *outer2Reference;
/* create 20 outer structs (should check for out-of-mem error) */
outerArray = malloc(20 * sizeof(outer));
for (i = 0; i < 10; ++i) {
outerReference = outerArray + i; // ptr to i'th outer
// Note: innerStruct.address bcz it's a structure
sprintf(outerReference->innerStruct.address, "outer struct %d", i);
}
/* create 20 outer2 structs */
outer2Array = malloc(20 * sizeof(outer2));
/* for each outer struct, dynamically allocate 10 inner structs */
for (i = 0; i < 10; ++i) {
outer2Reference = outer2Array + i;
outer2Reference->innerStruct = malloc(sizeof(inner));
// Note: innerStruct->address bcz it's a pointer
sprintf(outer2Reference->innerStruct->address, "outer2 struct %d", i);
}
/* print all the data and free malloc'ed memory */
for (i = 0; i < 10; ++i) {
printf("outer: %-20s, outer2: %-20s\n",
outerArray[i].innerStruct.address,
outer2Array[i].innerStruct->address);
free(outer2Array[i].innerStruct);
}
free(outer2Array);
free(outerArray);
return 0;
}