2015年2月1日日曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の5章(構造体、共用体、ビットフィールド: 独自の構造体を使う)、エクササイズ(p.233)を解いてみる。

その他参考書籍

エクササイズ(p.233)

コード(BBEdit, Emacs)

sample233.c

#include <stdio.h>

typedef struct {
  float tank_capacity;
  int tank_psi;
  char const *suit_material;
} equipment;

typedef struct scuba {
  char const *name;
  union {
    equipment;
    equipment kit;
  };
} diver;

void badge(diver d){
  printf("名前:%s タンク:%2.2f(%i) スーツ:%s\n",
         d.name, d.tank_capacity, d.tank_psi, d.kit.suit_material);
}

int main(){
  diver randy = {.name="ランディ", .tank_capacity=5.5, .tank_psi=3500,
                 .suit_material="ネオプレン"};
  badge(randy);
}

入出力結果(Terminal)

$ crun.sh sample233
clang ...
sample233.c:12:5: warning: anonymous structs are a Microsoft extension
      [-Wmicrosoft]
    equipment;
    ^~~~~~~~~
1 warning generated.
名前:ランディ タンク:5.50(3500) スーツ:ネオプレン
$

0 コメント:

コメントを投稿