構造体はいろんな種類のデータを一つにまとめることができる集合です
構造体内の各変数にはドットを使ってアクセスします
(構造体名は省略した書き方もできます)
例
構造体内の各変数にはドットを使ってアクセスします
(構造体名は省略した書き方もできます)
struct 構造体名 { 変数の型 変数; 変数の型 変数; 変数の型 変数; ・ ・ } 構造体の変数;
例
#include <stdio.h> #include <stdlib.h> struct VectorType2 { int x; int y; int length; } vo2; struct VectorType2 vec2; vec2.x = 5; vec2.y = 10; vec2.length = 2; printf("x is %d\n", vec2.x); vo2.x = 5; vo2.y = 10; vo2.length = 2; printf("x is %d\n", vo2.x); >>5
typedefを使うことでstructキーワードを省略して型名だけで宣言できます
注:これはC言語の場合でC++言語の場合はtypedefを使わなくてもstructを省略して宣言できます
注:これはC言語の場合でC++言語の場合はtypedefを使わなくてもstructを省略して宣言できます
typedef struct { 変数の型 変数; 変数の型 変数; 変数の型 変数; ・ ・ } 構造体の型名;例
#include <stdio.h> #include <stdlib.h> typedef struct { int x; int y; int length; } Vector2Type; Vector2Type vec2; vec2.x = 5; vec2.y = 10; vec2.length = 2; printf("x is %d\n", vec2.x); >>5
コメントをかく