SW/C C++7 [C/C++] 구조체2 구조체 포인터 동적메모리 이용 #include #include #include #include struct member { char name[20]; int age; }; void show_members(struct member *p, int size) { for(int i=0; iname, (p+i)->age); } } int main() { system("chcp 65001"); //gcc 한글깨짐 방지 printf("\n"); struct member *p; // 동적할당 p = (member *)malloc(sizeof(member) *4); // 구조체의 크기 : char 20개, int 1개 이므로 총 24바이트 // 4가 곱해져있으므로 맴버 4명 생성 if(p == NULL) { printf.. 2023. 1. 8. [C/C++] 구조체1 구조체 객체지향 언어에서 클래스와 유사 서로 다른 자료형을 한번에 다루기 위해 사용 ※ 배열의 경우는 같은 자료형 #include #include //system #include //strcpy struct student { int age; char major[20]; }; int main() { system("chcp 65001"); //gcc 한글깨짐 방지 printf("\n"); student a; a.age = 21; strcpy(a.major, "전자공학"); student b; b.age = 22; strcpy(b.major, "기계공학"); printf("a는 %d살 이고 전공은 %s 이다.\n", a.age, a.major); printf("b는 %d살 이고 전공은 %s 이다.\n", b... 2023. 1. 8. [C/C++] 포인터 문자열 포인터로 문자열 출력 문자열의 경우 마지막에 '\0' NULL 존재 #include #include int main() { system("chcp 65001"); //gcc 한글깨짐 방지 printf("\n"); char mystring[100]; char *p = &mystring[0]; printf("입력 : "); scanf("%s", &mystring); printf("출력 : "); while(1) { if(*p != '\0') { printf("%c", *p); p = p + sizeof(char); } else { break; } } printf("\n"); return 0; } 2023. 1. 7. [C/C++] Bubble Sort 버블정렬 가장 큰 값을 맨 오른쪽으로 바꾸면서 정렬 #include #include //rand() 함수 #include //time() 함수 #include int main() { system("chcp 65001"); // gcc 한글깨짐 방지 srand(time(NULL));// rand()을 진짜 랜덤으로 만들기 위해 필요 int random_array[20]; int random_array_size = sizeof(random_array)/4; // int형이므로 /4 printf("정렬전\n"); for(int i=0; i 2023. 1. 7. 이전 1 2 다음