Language/C
[따배씨] 11.3 문자열의 배열
migrationArc
2021. 6. 9. 17:21
따배씨 - 따라하며 배우는 C언어
11강 문자열 함수들
11.3 문자열의 배열
#include <stdio.h>
int main(){
const char* mythings[5] = {
"Dancing in the rain",
"Couting apples",
"Watching movies with friends",
"Writing sad letters",
"Studying the C language"
};
char yourthings[5][40] = {
"Studying the C++ language",
"Eating",
"Watching Netflix",
"Walking around till dark",
"Deleting spam emails"
};
const char* temp1 = "Dancing in the rain";
const char* temp2 = "Studying the C++ language";
printf("%s %u %u\n", mythings[0], (unsigned)mythings[0], (unsigned)temp1);
// Dancing in the rain 15956 15956
printf("%s %u %u\n", yourthings[0], (unsigned)yourthings[0], (unsigned)temp2);
// Studying the C++ language 4022334272 16064
return 0;
}
printf("%s %u %u\n", mythings[0], (unsigned)mythings[0], (unsigned)temp1);
// Dancing in the rain 15956 15956
- mythings 와 temp1 은 선언할때 pointer 로 선언
- Compiler 가 같은 내용임을 인지하여 한번만 저장 하고 주소값으로 연결
printf("%s %u %u\n", yourthings[0], (unsigned)yourthings[0], (unsigned)temp2);
// Studying the C++ language 4022334272 16064
- yourthings 는 배열로 선언, Stack 에 배열로 메모리 공간을 할당
- "Studying the C++ language" 는 별도 메모리에 복사해서 들어가는 형태
- 같은 String 이지만, 배열로 선언하였기 때문에 temp2 와 같은 주소가 아니다
#include <stdio.h>
int main(){
const char* mythings[5] = {
"Dancing in the rain",
"Couting apples",
"Watching movies with friends",
"Writing sad letters",
"Studying the C language"
};
char yourthings[5][40] = {
"Studying the C++ language",
"Eating",
"Watching Netflix",
"Walking around till dark",
"Deleting spam emails"
};
printf("%-30s %-30s\n", "My Things:", "Your Thins:");
for (int i = 0; i < 5; ++i){
printf("%-30s %30s\n", mythings[i], yourthings[i]);
}
return 0;
}
- %-30s, 줄맞추기 옵션
#include <stdio.h>
int main(){
const char* mythings[5] = {
"Dancing in the rain",
"Couting apples",
"Watching movies with friends",
"Writing sad letters",
"Studying the C language"
};
char yourthings[5][40] = {
"Studying the C++ language",
"Eating",
"Watching Netflix",
"Walking around till dark",
"Deleting spam emails"
};
printf("\nsizeof mythings: %zd, sizeof your yourthings: %zd\n", sizeof(mythings), sizeof(yourthings));
// sizeof mythings: 40, sizeof your yourthings: 200
return 0;
}
const char* mythings[5]
- char type 포인터 5개를 원소로 갖는 Array
- size == 40 byte, 8 byte * 5 = 40 byte
printf("%zd\n", sizeof(char*));
// 8
char yourthings[5][40]
- 40글자가 저장되는 String 5개를 원소로 갖는 Array
- size == 200 byte, 40 byte * 5 = 200 byte
- String 을 pointer 로 사용하면, 같은 내용을 여러번 사용시 하나의 메모리를 중복으로 사용하기 때문에 메모리 공간을 아낄수 있음
- 단, 메모리에 직접 접근하는 방식이므로 값을 변경하면 안됨.
- const 로 선언하자
#include <stdio.h>
int main(){
const char* mythings[5] = {
"Dancing in the rain",
"Couting apples",
"Watching movies with friends",
"Writing sad letters",
"Studying the C language"
};
char yourthings[5][40] = {
"Studying the C++ language",
"Eating",
"Watching Netflix",
"Walking around till dark",
"Deleting spam emails"
};
for (int i = 0; i < 100; ++i){
printf("%c", mythings[0][i]);
}
// Dancing in the rain Couting apples Watching movies with friends Writing sad letters Studying the C l
printf("\n\n");
for (int i = 0; i < 200; ++i){
printf("%d", (int)yourthings[0][i]);
}
// 831161171001211051101033211610410132674343321089711010311797103101000000000000000699711610511010300000000000000000000000000000000008797116991041051101033278101116102108105120000000000000000000000000879710810710511010332971141111171101003211610510810832100971141070000000000000000681011081011161051101033211511297109321011099710510811500000000000000000000
printf("\n\n");
for (int i = 0; i < 200; ++i){
printf("%c", yourthings[0][i]);
}
// Studying the C++ language''''''''''Eating''''''''''''''''''''''''''''''''''Watching Netflix''''''''''''Walking around till dark''''''''''Deleting spam emails''''''''''''''''''''''
printf("\n\n");
return 0;
}
- 주소값을 이용하여 출력, 권장하지 않는 방법
이 글의 모든 사진과 내용의 출처는 홍정모 교수님께 있음을 알려드립니다.
http://blog.naver.com/atelierjpro
실리콘 밸리의 프로그래머 : 네이버 블로그
안녕하세요! 홍정모 블로그에 오신 것을 환영합니다. 주로 프로그래밍 관련 메모 용도로 사용합니다. 강의 수강하시는 분들은 홍정모 연구소 카페로 오세요.
blog.naver.com
http://www.inflearn.com/course/following-c
홍정모의 따라하며 배우는 C언어 - 인프런 | 강의
'따배씨++'의 성원에 힘입어 새롭게 개발된 C 언어로 시작하는 프로그래밍 입문 강의입니다. '따배씨'와 함께 프로그래밍 인생을 업그레이드 해보세요., 따라하며 배우는 C언어 '따배씨++'의 성원
www.inflearn.com