LoginSignup
2
1

【C言語】構造体の宣言と型変換

Last updated at Posted at 2022-05-10

構造体の宣言

基本形は以下のようになる。

struct Hoge {
    int a; 
    int b;
    int c;
};

このとき宣言した構造体の変数宣言は以下のようになる

struct Hoge hoge;

構造体の型変換

いちいちstructを付けるのは面倒なので、Javaのオブジェクト宣言のように、型はHogeと書くだけで宣言できるようにしたい。先述した構造体を型変換するならば、以下の様に記述すれば、

typedef struct Hoge Hoge 

以下のように変数を宣言できる。

Hoge hoge;

構造体の宣言と型変換を同時に行う

以下のように宣言すれば良い

typedef struct {
    int a; 
    int b;
    int c;
} Hoge;

もちろんこのとき、変数宣言は以下のようになる

Hoge hoge;
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1