ProgrammingのTipなど

構造体

構造体はいろんな種類のデータを一つにまとめることができる集合です
構造体内の各変数にはドットを使ってアクセスします
(構造体名は省略した書き方もできます)
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
型名からstructを省略させる書き方
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

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Menu

メニュー2

開くメニュー

閉じるメニュー

  • アイテム
  • アイテム
  • アイテム
【メニュー編集】

管理人/副管理人のみ編集できます