1 / 33

C study

C study. Day 3 ~ Array. Written by Chang-young Koh. Today’s topic is. 배열 Array. 매우 중요해요 ! ( 아마 이 스터디에서 배우는 것중 가장 !) 그러니 포기하지 말고 끝까지 !. Index. 배열을 왜 쓰나 ? 배열의 기본적인 사용법 . 배열과 주소 , 포인터 . 약간은 특별한 배열 , 문자열. Why arrays?. 10 명의 학점을 입력받아 평균을 출력할 때.

Download Presentation

C study

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C study Day 3 ~ Array Written by Chang-young Koh.

  2. Today’s topic is.. 배열Array. • 매우 중요해요! • (아마 이 스터디에서 배우는 것중 가장!) • 그러니 포기하지 말고 끝까지!

  3. Index • 배열을 왜 쓰나? • 배열의 기본적인 사용법. • 배열과 주소,포인터. • 약간은 특별한 배열, 문자열.

  4. Why arrays? • 10명의 학점을 입력받아 평균을 출력할 때 • scanf(“%d%d%d%d%d%d%d%d%d%d”,&a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8,&a9,&a10);

  5. Why arrays? • 100명의 학점을 입력받아 평균을 출력할 때 • int score[100], i ; • for(i = 0 ; i < 100; i++) • scanf(“%d”,&score[i]);

  6. Why arrays? • 사용자의 학번을 입력받아 저장할 때 • scanf(“%d”,&id);

  7. Why arrays? • 사용자의 이름을 입력받아 저장할 때 • char name[22]; • scanf(“%s”,name);

  8. How to use arrays? - Declaration • 배열의 선언 • int score[100]; • char name[22]; • 변수랑 거의 똑같다! • 자료형 이름[크기]; - 크기는 변경 불가ㅠㅠ

  9. How to use arrays? - Initialization • 배열의 초기화 • int score[100] = {1,2,3,4}; • 이렇게 하면, score[0] = 1, score[1] = 2 • 이런 식으로 들어가는데, • 정해주지 않은 score[4]부터는 모두 0이 된다.

  10. How to use arrays? - Access • 배열에 접근하는 방법 • int score[100] , i; • for(i = 0 ; i < 100 ; i++) • scanf(“%d”,&score[i]); • int sum = 0; • for(i = 0 ; i < 100 ; i++) • sum + = score[i]; • 역시 배열이랑 똑같다!

  11. How to use arrays? - Access (advanced) • 배열에 접근하는 방법 (심화) • int score[100], i; • for(i = 0; i < 100; i++) • scanf(“%d”,score + i); • int sum = 0; • for(i = 0; i < 100; i++) • sum += *(score+i); • 결과는 앞에서와 완벽히 같다.

  12. How to use arrays? - Access (advanced) • 배열에 접근하는 방법 (심화) • 솔직히 무슨 말인지 이해가 안 되죠? • @_@...

  13. Pointer - Introduction • 여기서 짚고 넘어가는 포인터Pointer! • 사실 포인터는 별거 없어요. • 그냥 ‘주소’니까요. • 근데 이것만 잘 하면 2학기가 편해지죠. • 배열이랑도 관계가 많으니 열심히 해봅시다!

  14. Pointer - Declaration • int *a; • /*별을 붙이면 포인터!*/ • int b; • a = &b; • scanf(“%d”,a); • printf(“b = %d= %d”,b,*a);

  15. Pointer - Access • printf(“b = %d= %d”,b,*a); • /* a에 별을 붙인 *a는 • a가 가리키는 주소(&b) 의 값, 즉 • b의 값을 나타낸다 */

  16. Pointer - Why Pointer? • void swap1(int a, int b){ • int c = a; • a = b; • b = c; • return; • }

  17. Pointer - Why Pointer? • void swap2(int *a, int *b){ • int c = *a; • *a = *b; • *b = c; • return; • }

  18. Pointer - Why Pointer? • int x = 1000, y = 3; • swap1(x,y); • printf(“x = %d, y = %d\n”,x,y); • output : • x = 1000, y = 3 • 바뀌지 않았다!

  19. Pointer - Why Pointer? • int x = 1000, y = 3; • swap2(&x,&y); • printf(“x = %d, y = %d\n”,x,y); • output : • x = 3, y = 1000 • !!!

  20. Pointer - Why Pointer? • swap1이 왜 안될까? • void swap1(int a, int b)은, • swap1(x,y) 을 호출할 때 • a = x; • b = y; 와 같이 호출된다고 보면 된다. • 그러니까, 값이 복사된다 이 말이지!

  21. Pointer - Why Pointer? • 그럼 swap2는 왜 될까? • void swap2(int *a, int *b)은, • 아까 배웠듯이! • swap(&x,&y)를 호출하면 • a = &x; • b = &y; • 와 같이 호출된다고 보면 된다. • 그러니까, 주소가 복사된다이 말이지.

  22. Pointer - Summary • - 포인터의 선언은 int *a; • 와 같은 식으로변수명에*을 붙인다. • int *a를 선언했으면 • a는 주소, *a는 그 주소에 담긴 값에 접근할 때 쓴다!

  23. Pointer - Summary • - 포인터의 선언은 int *a; • 와 같은 식으로변수명에*을 붙인다. • int *a를 선언했으면 • a는 주소, *a는 그 주소에 담긴 값에 접근할 때 쓴다! • - 이 외에도 포인터에 관해선 할 말이 정말 많지만, 그건 다음 기회에!

  24. How to use arrays? - Access (advanced) • 배열에 접근하는 방법 (심화) • int score[100], i; • for(i = 0; i < 100; i++) • scanf(“%d”,score + i); • int sum = 0; • for(i = 0; i < 100; i++) • sum += *(score+i); • 결과는 앞에서와 완벽히 같다.

  25. How to use arrays? - Access (advanced) • 배열에 접근하는 방법 (심화) • int score[100]; • 이 경우, • score 은 &score[0] 와 같고, • score + 1 은 &score[1] 와 같다. • 어렵게 설명하면, • score + 1 = &score[0] + sizeof(int)*1 • 이란 말이지!

  26. How to use arrays? - Access (advanced) • 배열에 접근하는 방법 (심화) • int score[100]; • 그러니까, • (score + i) 는 &score[i]와 같고, • 당연히 *(score + i) 는 *(&score[i]), • 즉 score[i]와 같다!

  27. Array of characters • 약간은 특별한배열, ‘문자열’ . • char name[] = “Hello World!” • 문자열은 이렇게도 초기화 할 수 있다! • name[0] = ‘H’ • name[1] = ‘e’ • . • . • name[12] = ‘!’ • name[13] = ‘\0’

  28. Array of characters • 약간은 특별한배열, ‘문자열’ . • char name[] = “Hello World!” • name[12] = ‘!’ • name[13] = ‘\0’ • ‘\0’은 문자열의 끝을 나타내는 문자. • NULL문자라고 부른다.

  29. Array of characters • 약간은 특별한배열, ‘문자열’ . • char name[22]; • scanf(“%s”.name); • 이렇게 하면, • 띄어쓰기나 줄바꿈 문자 전까지의 문자를 • 모두 받아서 name 배열에 넣어준다. • 주의할 점은, 받을 문자열의 크기보다 배열의 크기를 1 크게 잡아야 한다는것! • (널 문자 자리!)

  30. Array of characters • 약간은 특별한배열, ‘문자열’ . • char name[] = “Hello World!”; • printf(“%s”,name); • 이렇게 하면, • 널 문자가 나오기 전까지의 문자를 모두 출력한다. • name이라는 배열의 ‘주소’ 가 인자로 들어가는 것에 주의!

  31. Array of characters • 약간은 특별한배열, ‘문자열’ . • 관련 헤더 파일 • string.h • cplusplus.com 에서 찾아볼 것! • 관련 함수 목록 • strlen : 문자열의 길이를 구한다. • strcpy : 문자열을 복사한다. • strcat : 두 문자열을 붙인다. • strcmp : 두 문자열을 비교한다. • …

  32. Summary • 배열의 선언 : intscore[100]; • 배열에 접근 : score[1], &score[1] • 배열에 접근(심화) : • (score + 1) = &score[1] • *(score + 1) = *(&score[1]) • = score[1] • 문자열 : 문자열의 끝을 나타내는 • 널 문자‘\0’, 그리고 %s

  33. Thank you!

More Related