# 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.

## Presentation on theme: "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."— Presentation transcript:

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 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 ’ } */

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 */

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

Pointer 에 의한 접근의 결과 Declarations and initializations int i=3, j=5, *p=&i, *q=&j, *r; double x; ExpressionEquivalent expressionValue p == & ip == (& i)1 * * & p* ( * ( & p))3 r = & xr = (& x)/* illegal */ 7 * * p / * q + 7(((7 * (* p))) / (* p)) + 711 * (r = & j) *= * p(* (r = (& j))) *= (* p)15

Legality of opertions Declarations int float void * p; * q; * v; Legal assignmentsIllegal assignments p = 0;p = 1; p = (int *) 1;v = 1; p = v = q;p = q; p = (int *) q;

주의할 점 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 ?????

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

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

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

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];

계속 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];

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

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)

Various ways that sum( ) might be called InvocationWhat gets computed and returned sum(v, 100)v[0] + v[1] + … c … + v[99] sum(v, 88)v[0] + v[1] + … c … + v[87] sum(&v[7], k – 7)v[7] + v[8] + … c … + v[k - 1] sum(v+7, 2 * k)v[7] + v[8] + … c … + v[2 * k+6] 수행 내용

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

p.258 Unordered data:73663-522-772 First pass:-7773663-5222 Second pass:-77-573663222 Third pass:-77-527366322 Fourth pass:-77-523736622 Fifth pass:-77-523372266 Sixth pass:-77-523372266 Seventh pass:-77-523372266

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 가 부족하면 )

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

Memory map automatic (stack) ↓↑↓↑ malloc (heap) static external

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

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

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

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 ’ };

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

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

예제 Declarations and initializations char s1[] = “ beautiful big sky country ”, s2[] = “ how now brown cow ” ; ExpressionValue strlen(s1)25 strlen(s2 + 8)9 strcmp(s1, s2)negative integer StatementsWhat gets printed printf( “ %s ”, s1 + 10);big sky country strcpy(s1 + 10, s2 + 8); strcat(s1, “ s! ” ); printf( “ %s ”, s1);beautiful brown cows!

Multidimensional arrays Examples of declarations of arraysRemarks int a[100]; b[2][7]; c[5][3][2]; a one-dimensional array a two-dimensional array a three-dimensional array

col 1col 2col 3col 4col 5 row 1a[0][0]a[0][1]a[0][2]a[0][3]a[0][4] row 2a[1][0]a[1][1]a[1][2]a[1][3]a[1][4] row 3a[2][0]a[2][1]a[2][2]a[2][3]a[2][4] Two-dimensional arrays int a[3][5]; /* row major */ row major  column major

Expressions equivalent to a[i][j] *(a[i] + j) (*(a + i)) [j] *((*(a + i)) + j) *(&a[0][0] + 5*i + j) 예제

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

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]

One dimensional array parameter int b[]  int b[3]  int *b char *argv[]  char *argv[3]  char **argv Caution ::: char x[][] char **x

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]

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};

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 프로그램 설명

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

Example

Swapping of two strings W w[j] w[i] ＼0＼0 elppa ＼0＼0 rof Before swapping W w[j] w[i] ＼0＼0 elppa ＼0＼0 rof After swapping

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

Arguments to main() int main(int argc, char *argv[]) { int i; printf( “ argc = %d\n ”, argc); for (i=0;i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/14/4258066/slides/slide_42.jpg", "name": "Arguments to main() int main(int argc, char *argv[]) { int i; printf( argc = %d\n , argc); for (i=0;i

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

예 A ragged array

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

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)

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 페이지 )

Finding a root Finding a root by bisection

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

Kepler equation A solution of the Kepler equation x0x0 x y y =x y =m+e sinx

Arrays of Pointers to Function function 은 array 의 처리와 유사 dbl (*pfdd) (dbl); pfdd = kepler; /* 302page 설명 */ Function 을 array 로 처리 pfdd f[N] = {NULL, f1, f2, f3}; /* 304page 설명 */

The type qualifier const (ANSI) static const int k=3;  can be initialized but can not be modified : K is a constant int with static storage class const int n=3; int v[n];  error const int a=7; int *p = &a;  ERROR const int a=7; const int *p =&a;  OK

The type qualifier volatile (ANSI ) A volatile object can be modified in some unspecified way by the hardware extern const volatile int real_time_clock;

숙제 수업시간 :::3, 6, 7,8, 11, 18, 24, 27, 38, 42 집 ::: 4, 10, 12, 16, 20, 32, 35 추가 ::: 다음 중 2 문제를 집에서 풀 것  29, 30,46