따라 배우는 C언어 : 15
🙇♀️15
🪐구조체
struct Patient
{
char name[MAX_NAME];
float height;
float weight;
int age;
}
struct Patient p1, p2, p3;
strcpy(p1.name, "seo");
p1.height = 174.0f;
p1.weight = 72.5f;
p1.age = 24;
🪐구조체의 기본적인 사용법
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 41
struct Person /* Person is a tag of strcutre*/
{
char name[MAX]; // member
int age; // member
float height; // member
};
int main()
{
int flag; // Receives return value of scanf()
/* Structre variable */
struct Person genie;
/* sot(.) is structure membership operator (member access operator, member operator) */
strcpy(genie.name, "Will Smith");
//strncpy(genie.name, "Will Smith", MAX);
genie.age = 1000;
// dot(.) has higher precedence than &
flag = scanf("%f", &genie.height); // &(genie.height)
printf("%f\n", genie.height);
/* Initialization */
struct Person princess = { "Naomi Scott", 18, 160.0f };
struct Person princess2 = {
"Naomi Scott",
18,
160.0f
};
strcpy(princess.name, "Naomi Scott");
princess.age = 18;
princess.height = 160.0f;
/* Designated initailizers */
struct Person beauty = {
.age = 18,
.name = "Bell",
.height = 150.0f
};
//struct Person beauty = { .age = 19, .name = "Bell",.height = 150.0f };
/* Pointer to a structure variable */
struct Person* someone;
someone = &genie;
//someone = (struct Person*)malloc(sizeof(struct Person)); // and free late
/* Indirect member(ship) operator (or structure pointer operator) */
someone->age = 1001; // arrow(->) operator
printf("%s %s\n", someone->name, (*someone).age);
/* Structure declarations in a function */
struct Book
{
char title[MAX];
float price;
};
/* No tag */
struct {
char farm[MAX];
float price;
} apple, apple2;
strcpy(apple.farm, "Trade Joe");
apple.price = 1.2f;
strcpy(apple2.farm, "Safeway");
apple2.price = 5.6f;
/* typedef and structure */
typedef struct Person my_person;
my_person p3;
typedef struct Person person;
person p4;
typedef struct {
char name[MAX];
char hobby[MAX];
} friend;
friend f4;
return 0;
}
🪐구조체의 배열 연습문제
#include <stdio.h>
#include <string.h>
#define MAX_TITLE 40
#define MAX_AUTHOR 40
#define MAX_BOOKS 3 /* maximum number of books */
char* s_gets(char* st, int n)
{
char* ret_val;
char* find;
ret_val = fgets(st, n, stdin); // vs. scanf()
if (ret_val)
{
find = strchr(st, '\n'); // look for newline
if (find) // if the address is not NULL
*find = '\0'; // place a null character there
else
while (getchar() != '\n')
continue; // dispose of rest of line
}
return ret_val;
}
struct book
{
char title[MAX_TITLE];
char author[MAX_AUTHOR];
float price;
};
int main()
{
struct book library[MAX_BOOKS] = { {"Empty", "Empty", 0.0f}, }; /* array of book structures */
int count = 0;
while (1)
{
printf("Input a book title or press [Enter] to stop.\n>>");
// TODO: input title
if (s_gets(library[count].title, MAX_TITLE) == NULL) break;;
// TODO: break if the first character of the input title is '\0'
if (library[count].title[0] == '\0') break;
printf("Input the author.\n>>");
// TODO: input author name
s_gets(library->author, MAX_AUTHOR);
printf("Input the price.\n>>");
// TODO: input price
int flag = scanf("%f", &library[count].price);
// TODO: clear buffer
while (getchar() != '\n')
continue; /* clear input line */
count++;
if (count == MAX_BOOKS)
{
printf("No more books.\n");
break;
}
}
if (count > 0)
{
printf("\nThe list of books.\n");
for (int index = 0; index < count; index++)
printf("\"%s\" written by %s: $%.1f\n",
library[index].title, library[index].author, library[index].price);
}
else
printf("No books to show.\n");
return 0;
}
🪐구조체와 함수 연습문제
#include <stdio.h>
#include <string.h>
#define NLEN 20
// 구조체와 함수 연습문제
// 나의 이름을 입력 받고 character수 와 함께 이름 출력
struct name_count
{
char first[NLEN]; // first name
char last[NLEN]; // last name
int num;
};
void receive_input(struct name_count*);
void count_characters(struct name_count*);
void show_result(const struct name_count*);
char* s_gets(char* st, int n);
int main()
{
struct name_count user_name;
receive_input(&user_name);
count_characters(&user_name);
show_result(&user_name);
return 0;
}
void receive_input(struct name_count* pter_nc)
{
int flag;
printf("Input your first name:\n>>");
flag = scanf("%[^\n]%*c", pter_nc->first); // ^ : cap operator
if (flag != 1)
printf("Wrong input");
printf("Input your last name:\n>>");
flag = scanf("%[^\n]%*c", pter_nc->last);
if (flag != 1)
printf("Wrong input");
}
void count_characters(struct name_count* ptr_nc)
{
ptr_nc->num = strlen(ptr_nc->first) + strlen(ptr_nc->last);
}
void show_result(const struct name_count* ptr_nc)
{
printf("Hi, %s %s, Your name has %d characters.\n",
ptr_nc->first, ptr_nc->last, ptr_nc->num);
}
위에건 포인터를 사용하는 방법
- 포인터를 사용하지 않고 반환값을 이용해서 만든 방법
#include <stdio.h>
#include <string.h>
#define NLEN 20
// 구조체와 함수 연습문제
// 나의 이름을 입력 받고 character수 와 함께 이름 출력
struct name_count
{
char first[NLEN]; // first name
char last[NLEN]; // last name
int num;
};
struct name_count receive_input();
struct name_count count_characters(struct name_count);
void show_result(const struct name_count);
char* s_gets(char* st, int n);
int main()
{
struct name_count user_name;
user_name = receive_input();
user_name = count_characters(user_name);
show_result(user_name);
return 0;
}
struct name_count receive_input()
{
struct name_count nc;
int flag;
printf("Input your first name:\n>>");
flag = scanf("%[^\n]%*c", nc.first); // ^ : cap operator
if (flag != 1)
printf("Wrong input");
printf("Input your last name:\n>>");
flag = scanf("%[^\n]%*c", nc.last);
if (flag != 1)
printf("Wrong input");
}
struct name_count count_characters(struct name_count nc)
{
nc.num = strlen(nc.first) + strlen(nc.last);
}
void show_result(const struct name_count nc)
{
printf("Hi, %s %s, Your name has %d characters.\n",
nc.first, nc.last, nc.num);
}