Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
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 31
Tags
more
Archives
Today
Total
관리 메뉴

문타쿠, 공부하다.

[C언어 코딩 도장] Unit 52. 연습문제 및 심사문제 본문

C언어/C언어 코딩 도장

[C언어 코딩 도장] Unit 52. 연습문제 및 심사문제

개발새발 문타쿠 2023. 12. 8. 00:26

52.4 연습문제: 2차원 좌표 초기화하기

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

struct Point2D
{
	int x;
	int y;
};

int main(void)
{
	struct Point2D p;
	struct Point2D* ptr = malloc(sizeof(struct Point2D));

	memset(&p, 0, sizeof(struct Point2D));
	memset(ptr, 0, sizeof(struct Point2D));

	printf("%d %d %d %d\n", p.x, p.y, ptr->x, ptr->y);

	return 0;
}

52.5 연습문제: 2차원 좌표 복제하기

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

struct Point2D
{
	int x;
	int y;
};

int main(void)
{
	struct Point2D p1;
	struct Point2D* p2 = malloc(sizeof(struct Point2D));

	p1.x = 10;
	p1.y = 20;

	memcpy(p2, &p1, sizeof(struct Point2D));

	printf("%d %d\n", p2->x, p2->y);

	free(p2);

	return 0;
}

52.6 심사문제: 인적 정보 삭제하기

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

struct Person
{
	char name[20];
	char address[100];
	int age;
};

int main(void)
{
	struct Person p1;

	strcpy_s(p1.name, sizeof(p1.name), "홍길동");
	strcpy_s(p1.address, sizeof(p1.address), "서울시 용산구 한남동");
	p1.age = 30;

	printf("인적 정보 삭제 전\n");
	printf("이름: %s\n", p1.name);
	printf("나이: %d\n", p1.age);
	printf("주소: %s\n\n", p1.address);

	memset(&p1, 0, sizeof(struct Person));

	printf("인적 정보 삭제 후\n");
	printf("이름: %s\n", p1.name);
	printf("나이: %d\n", p1.age);
	printf("주소: %s\n", p1.address);

	return 0;
}


52.7 심사문제: 인적 정보 복제하기

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

struct Person
{
	char name[20];
	char address[100];
	int age;
};

int main(void)
{
	struct Person* p1 = malloc(sizeof(struct Person));
	struct Person p2;

	strcpy_s(p1->name, sizeof(p1->name), "고길동");
	strcpy_s(p1->address, sizeof(p1->address), "서울시 서초구 반포동");
	p1->age = 40;

	memcpy(&p2, p1, sizeof(struct Person));

	printf("이름: %s\n", p2.name);
	printf("나이: %d\n", p2.age);
	printf("주소: %s\n", p2.address);

	free(p1);

	return 0;
}