1 / 54

8 주 강의

8 주 강의. Arrays, Pointers, and Strings. One dimensional array. int grad0, grad1, grad2;  int grad[3]; int a[ size ]; /* a[0], … , a[ size-1 ] #define N 100 int a[N]; /* a[0] .. a[99] is allocated */ for(i=1; i<100; ++i) sum += a[i];. Initialization.

inez
Download Presentation

8 주 강의

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. 8주 강의 Arrays, Pointers, and Strings

  2. One dimensional array • int grad0, grad1, grad2;  int grad[3]; • int a[size]; /* a[0], …, a[size-1] • #define N 100 int a[N]; /* a[0] .. a[99]is allocated */ for(i=1; i<100; ++i) sum += a[i];

  3. Initialization float f[5]={0.0, 1.0, 2.0, 3.0, 4.0}; int a[100]={0}; /* all elements to 0 */ int a[]={2,3,5,-7}; /* int a[4] … */ char s[] = “abc”; /*  char s[] = {‘a’, ‘b’, ‘c’, ‘\0’} */

  4. Pointers • &v는 변수 v의 location(주소) • int *p ::: declares p to be type pointer to int • p=0;  p=NULL; • p=&i; • p=(int *) 1776 • /* an absolute address */

  5. Pointer 예 int a, b, *p; a=1; b=2; p=&a;

  6. Pointer에 의한 접근의 결과

  7. Legality of opertions

  8. 주의할 점 • 7 * * p / * q + 7 • ??? 7 * * p /* q +7 --trouble making • &3 /* illegal */ • &(k+3) /* illegal */ • register v; &v /* illegal */ • &a[0], &a[i+j+3] /* legal */ • int a[3]; &a ?????

  9. Call by reference (address) • To change the values of variables in the calling environment • Simulating the effect of call by reference by pointers

  10. • void swap(int *p, int *q) { int tmp; tmp = *p; *p = *q; *q = tmp } • int i=3, j=5; swap(&i, &j);

  11. The simulation of call by reference • Declaring a function parameter to be a pointer • Using the de-referenced pointer in the function body • Passing an address as an argument when the function is called

  12. The relationship between arrays and pointers • An array name itself is an address, or pointer value, and pointers as well as arrays, can be subscripted • a[i]  *(a+i) • #define N 100 int a[N], I, *p, sum=0;  p=a;  p=&a[0];  p=a+1  p=&a[1];

  13. 계속 • for (p=a; p < &a[N]; ++p) sum += *p;  for(i = 0; i < N; ++i) sum += *(a + i);  p=a; for (i=0; i < N; ++i) sum += p[i];

  14. Pointer 연산 • int *a; short *b; float *c; double *d; a+1  주소 4 증가 b+1  주소 2 증가 c+1  주소 4 증가 d+1  주소 8 증가

  15. Arrays as function arguments • double sum(double a[], int n) { int i; double sum = 0.0; for (i = 0; i < n; ++i) sum+=a[i]; return sum; }  double sum(double *a, int n)

  16. 수행 내용

  17. Bubble sorting • Greedy method • 큰 것을 먼저 뒤로 보내거나, 작은 것을 먼저 앞으로 보내거나 • 257(6.7절)페이지 설명

  18. p.258

  19. calloc() and malloc() • stdlib.h • Dynamic memory allocation (calloc : contiguous allocation, malloc : memory allocation) • Dynamically create space for arrays, structures, unions • callc(n, el_size) • malloc(n*el_size) • 실패시 NULL을 return (memory가 부족하면)

  20. • int *a, n; … a=calloc(n, sizeof(int)); • a=malloc(n*sizeof(int)); • deallocation : free(ptr) free(a); • Reallocation : realloc() • 261page 프로그램 설명

  21. Memory map

  22. Offsetting the pointer a = calloc(n, sizeof(double));

  23. Offsetting the pointer a = calloc(n+1, sizeof(double)) ;

  24. Merge sorting • Merge to arrays • Merge방법 설명 (264page 프로그램) • Merge sorting 방법 설명 (266 page) • Why power of 2? • 그러면 해결방법은

  25. Strings • Strings are one-dimensional array of type char • A null character is a byte with all bits off (\0) • char *p = “abc”; • A string constant is treated as a pointer • “abc”[1]  *(“abc”+1) • char *p=“abcde”; • char s[] = “abcde”;  char s[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘\0’};

  26. The memory map - 272 페이지의 word_count program 설명

  27. String handling functions in the standard library • char *strcat(char *s1, const char *s2); • int strcmp(const char *s1, const char *s2); • char *strcpy(char *s1, const char *s2); • size_t strlen(const char *s); /* unsigned int */ • strlen()와 strcpy()의 프로그램 (page 273, 274) • Strcat() 프로그램  275page

  28. 예제

  29. Multidimensional arrays

  30. Two-dimensional arrays int a[3][5]; /* row major */ row major  column major

  31. 예제

  32. Storage mapping function • int a[3][5] • a[i][j]  *(&a[0][0] + 5*i +j) • int a[I][J][K} • a[i][j][k]  *(&a[0][0][0] + I*J*i + J*j +k

  33. Formal parameter declarations • int a[3][5] • int sum(int a[][5]) { int i,j,sum=0; for(i=0;i<3;++i) for(j=0,j<5;++j) sum +=a[i][j]; return sum; } • int a[][5]  int a[3][5]  int (*a)[5]

  34. One dimensional array • parameter int b[]  int b[3]  int *b • char *argv[]  char *argv[3]  char **argv • Caution ::: char x[][] <=/=> char **x

  35. Three dimensional array • int a[7][9][2]; • a[i][j][k]  *(&a[0][0][0] + 9*2*I + 2*j + k) • Parameter ::: int a[][9][2]  int a[7][9][2]  int (*a)[9][2]

  36. Initialization • int a[2][3] = {1,2,3,4,5,6};  int a[2][3] = {{1,2,3},{4,5,6}};  int a[ ] [3] = {{1,2,3},{4,5,6}}; • 뒤의 0은 쓰지 않아도 int a[2][2][3] = { {{1,1,0}, {2,2,0}} {{3,0,0}, {4,4,0}} };  int a[][2][3] = {{{1,1}, {2}}, {3}, {4,4}}}; • int a[2][2][3] = {0};

  37. The use of typedef • Self documentation • #define N 3] typedef double scalar; typedef scalar vector[N]; typedef scalar matrix[N][N]; scalar a; vector b; matrixc; • page 283 프로그램 설명

  38. Arrays of pointers • Sorting strings :: a pointer of a pointer • 285page 설명

  39. Example

  40. Before swapping After swapping W W w[i] f o r \0 w[i] f o r \0 w[j] a p p l e \0 w[j] a p p l e \0 Swapping of two strings

  41. 숙제 • 사람의 이름을 입력하여 순서화한다 - 1수준 ::: 순서화만 - 2수준 ::: 사람 이름 대신에 주소 - 3수준 ::: 한글 이름을 입력하여 순서화

  42. Arguments to main() • int main(int argc, char *argv[]) { int i; printf(“argc = %d\n”, argc); for (i=0;i<argc;++i) printf(argv[%d] = %s\n”, i argv[i]); return 0; } • my_echo a is an apple • argv=5, argv[0]= my_echo … argv[4] = apple

  43. Ragged arrays • char a[2][15] = {“abc:”, “a is for apple”}; char *p[2] = {“abc:”, “a is for apple”}; • Ragged array ::: an array of pointers whose elements are used to point varying sizes

  44. A ragged array

  45. Functions as arguments #include “sum_sqr.h” double sum_square (double f(double x), int m, int n) { int k; double sum=0.0; for (k=m; k<=n;++k) sum +=f(k)*f(k); return sum; } • double sum_square(double (*f)(double), int m, int n); • sum += (*f)(k) * (*f)(k); 도 가능 • 조심 :::: double sum_square(double *f(double), int m, int n)은 안 된다 …함수 f(double)의 return 값이 double의 pointer

  46. Function prototypes • Function prototypes double sum_square (double f(double x), int m, int n) double sum_square (double f(double), int m, int n) double sum_square (double f(double x), int, int) double sum_square (double (*f)(double x), int, int) double sum_square (double (*)(double x), int, int) double sum_square (double g(double x), int a int b) double sum_square (double f(double x0, int m, int n) double sum_square (double f(double x0, int m, int n)

  47. Using bisection to find the root of a function • A real number x that satisfies the equation f(x) = 0  a root of f • 프로그램 설명 (298페이지)

  48. Finding a root by bisection Finding a root

  49. The Kepler equation • m = x – e sin(x)  y=x and y=m + e sin(x)  x – e sin(x) – m = 0

  50. A solution of the Kepler equation y y =m+e sinx y =x x x0 Kepler equation

More Related