Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- server
- Math
- php
- graph
- sorting
- JavaScript
- 인프런
- dfs
- Algorithm
- BOJ
- 정수론
- BASIC
- 따배씨
- 종만북
- udemy
- 따라하면서 배우는 C언어
- C
- 따라하며 배우는 C언어
- programmers
- Algospot
- String
- greedy
- BFS
- C언어
- 백준
- Python
- DP
- web
- 생활코딩
- Cleancode
Archives
- Today
- Total
몽상실현개발주의
[따배씨] 14.17 익명 공용체 본문
따배씨 - 따라하며 배우는 C언어
14강 구조체_2
14.17 익명 공용체 Anonymous Unions
#include <stdio.h>
/*
Unions And Structures
*/
struct personal_owner
{
char rrn1[7]; //Resident Registration Number
char rrn2[8]; //ex: 830422-1185600
};
struct company_owner
{
char crn1[4]; //Company Registraton Number
char crn2[3]; //ex: 111-22-33333
char crn3[6];
};
struct car_data
{
char model[15];
int status; /* 0 = personal, 1 = company */
union
{
struct personal_owner po;
struct company_owner co;
};
};
void print_car(struct car_data car)
{
printf("---------------------------------------\n");
printf("Car model : %s\n", car.model);
if (car.status == 0) /* 0 = personal, 1 = company */
{
printf("Personal owner : %s-%s\n", car.po.rrn1, car.po.rrn2);
}
else
{
printf("Company owner : %s-%s-%s\n", car.co.crn1, car.co.crn2, car.co.crn3);
}
printf("---------------------------------------\n");
}
int main()
{
struct car_data my_car = {.model = "Avante", .status = 0, .po = {"830422", "1185600"}};
struct car_data company_car = {.model = "Sonata", .status = 0, .co = {"111", "22", "333"}};
print_car(my_car);
print_car(company_car);
return 0;
}
- ownerinfo union 을 Anonymous Union 으로 변경
#include <stdio.h>
int main(){
struct Vector2D{
union{
struct { double x, y; };
struct { double i, j; };
struct { double arr[2]; };
};
};
typedef struct Vector2D vec2;
vec2 v = { 3.14, 2.99 };
printf("%.2f %.2f\n", v.x, v.y);
// 3.14 2.99
printf("%.2f %.2f\n", v.i, v.j);
// 3.14 2.99
printf("%.2f %.2f\n", v.arr[0], v.arr[1]);
// 3.14 2.99
return 0;
}
- 좌표값을 x, y / i, j / arr[0], arr[1] 의 형태로 모두 사용가능 하게 된다.
이 글의 모든 사진과 내용의 출처는 홍정모 교수님께 있음을 알려드립니다.
http://blog.naver.com/atelierjpro
실리콘 밸리의 프로그래머 : 네이버 블로그
안녕하세요! 홍정모 블로그에 오신 것을 환영합니다. 주로 프로그래밍 관련 메모 용도로 사용합니다. 강의 수강하시는 분들은 홍정모 연구소 카페로 오세요.
blog.naver.com
http://www.inflearn.com/course/following-c
홍정모의 따라하며 배우는 C언어 - 인프런 | 강의
'따배씨++'의 성원에 힘입어 새롭게 개발된 C 언어로 시작하는 프로그래밍 입문 강의입니다. '따배씨'와 함께 프로그래밍 인생을 업그레이드 해보세요., 따라하며 배우는 C언어 '따배씨++'의 성원
www.inflearn.com
'Language > C' 카테고리의 다른 글
[따배씨] 14.19 열거형 연습문제 (0) | 2021.07.07 |
---|---|
[따배씨] 14.18 열거형 (0) | 2021.07.06 |
[따배씨] 14.16 공용체와 구조체를 함께 사용하기 (0) | 2021.07.05 |
[따배씨] 14.15 공용체 Union 의 원리 (0) | 2021.07.05 |
[따배씨] 14.14 구조체 파일 입출력 연습문제 (0) | 2021.07.04 |
Comments