# include <stdio.h> # include <stdlib.h> // 構造体の宣言 typedef struct { int num; char *str; } strct; int main(void) { // 実体を生成 strct *entity; // 動的メモリの確保。確保したメモリをstrct型ポインタにキャスト。 entity = (strct*)malloc(sizeof(strct)); // メンバの初期化 entity->num = 0; entity->str = (char*)malloc(sizeof(32)); // メモリに文字列を代入 sprintf(entity->str, "%s %s!", "Hello", "World"); printf("%s\n", entity->str); // メモリの解放 free(entity->
