본문 바로가기

ETC Programmings

구조체 배워보기..

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct food {
 char name[15];
 int portion_weight;
 int calories;
} meal1, meal2, meal3;

int result(){
 return meal1.calories + meal2.calories + meal3.calories;
}
// 얘는 작동안함.
void init(struct food setMeal, char *setName, int setWeight, int setCalories){
 strcpy(setMeal.name, setName);
 setMeal.portion_weight = setWeight;
 setMeal.calories = setCalories;
}
void main(){
 strcpy(meal1.name, "Apple");
 meal1.portion_weight = 120;
 meal1.calories = 300;
 strcpy(meal2.name, "Screambled Egg");
 meal2.portion_weight = 12;
 meal2.calories = 100;
 strcpy(meal3.name, "Rice");
 meal3.portion_weight = 300;
 meal3.calories = 450;
 
 printf("%s   %d %d\n", meal1.name, meal1.portion_weight, meal1.calories);
 printf("%s  %d %d\n", meal2.name, meal2.portion_weight, meal2.calories);
 printf("%s   %d %d\n", meal3.name, meal3.portion_weight, meal3.calories);
 printf("------------------------------------\n");
 printf("total    %d", result());
}


먼가 객체생성하는 것과 비슷하지만.. 'ㅅ';; 아직 잘 모르겠다. 암튼 저기 생성자처럼 만들어준건 작동안한다;