2013년 1월 23일 수요일

헤드 퍼스트 C 5강 (struct)


#include <stdio.h>

typedef struct fish{ //구조체 이름 fish
const char *name;
const char *species;
int teeth;
int age;
}turtle; //별명 turtle

void catalog(turtle f) //별명으로 접근
{
printf("%s는  %s종이며, 이빨이 %i개이고, %i살 입니다.\n", f.name, f.species, f.teeth, f.age);
}

void label (struct fish f) //구조체 이름으로 접근
{
printf("이름 : %s\n", f.name);
printf("종 : %s\n", f.species);
printf("이빨 수 : %i\n", f.teeth);
printf("나이 : %i\n", f.age);
}

void happy_birthday(turtle *t) //포인터로 받음
{
(*t).age = (*t).age + 1; //포인터 주소를 통한 연산
printf("생일 축하해 %s! 이제 %i살이 되었네!\n", t->name, t->age); // -> 이 방법도 있음
}

int main()
{
turtle snappy = {"꼬북이", "장수거북", 15, 99};
happy_birthday(&snappy); //포인터 주소 읽기 &
printf("%s의 나이는 이제 %i살 입니다.\n", snappy.name, snappy.age);
catalog(snappy);
label(snappy);
return 0;
}

댓글 없음:

댓글 쓰기