Pointers nd Arrs CS 21 1 2 Pointers More Pointer Emples Wht is pointer A vrile whose vlue is the ddress of nother vrile p is pointer to vrile v Opertions &: ddress of (reference) *: indirection (dereference) p: v: 11 1000 2 11 References (e.g., *p) re vriles int,, *p, *p; Declrtion mimics use int *p; p is the ddress of n int (dereference p is n integer) int v; p = &v; p stores the ddress of v 0 4
More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ p p p p 5 6 More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p p p p 8
More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ 0 p 0 p p 9 p 10 More Pointer Emples More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ *p += 1; /* increments to 1 */ 0 0 p p 11 p p 12
More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ *p += 1; /* increments to 1 */ More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ *p += 1; /* increments to 1 */ = (*p)++; /* sets to 1, to 2 */ 1 1 p p 1 p p 14 More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ *p += 1; /* increments to 1 */ = (*p)++; /* sets to 1, to 2 */ More Pointer Emples References (e.g., *p) re vriles int,, *p, *p; p = &; /* p is the ddress of */ *p = 0; /* sets to 0 */ p = p; /* p lso points to */ *p += 1; /* increments to 1 */ = (*p)++; /* sets to 1, to 2 */ 1 1 2 1 p p 15 p p 16
Opertor Precedents Unr opertors ssocite right to left = *&; /* sme s = *(&) */ Unr opertors ind more tightl thn inr ones = *p + 1; /* sme s = (*p) + 1; */ More emples = *p++; /* sme s = *p; p++; */ = *(p++); /* sme s ove */ = *++p; /* sme s p++; = *p; */ = ++*p; /* sme s = (*p) + 1; */ When in dout, lierll use prentheses Argument Pssing C functions pss rguments vlue void swp(int, int ) t = ; = ; = t; int =, = ; swp(, ); printf( %d %d\n,,); 1 18 Argument Pssing C functions pss rguments vlue Argument Pssing C functions pss rguments vlue void swp(int, int ) t = ; = ; = t; int =, = ; swp(, ); printf( %d %d\n,,); void swp(int, int ) t = ; = ; = t; int =, = ; swp(, ); printf( %d %d\n,,); 19 20
Argument Pssing C functions pss rguments vlue To pss rguments reference, use pointers void swp(int, int ) t = ; = ; = t; int =, = ; swp(, ); printf( %d %d\n,,); void swp(int *, int t = *; * = *; * = t; int =, = ; swp(&, &); printf( %d %d\n,,); *) Argument Pssing C functions pss rguments vlue To pss rguments reference, use pointers void swp(int, int ) t = ; = ; = t; int =, = ; swp(, ); printf( %d %d\n,,); void swp(int *, int t = *; * = *; * = t; int =, = ; swp(&, &); printf( %d %d\n,,); *) 21 22 Argument Pssing C functions pss rguments vlue To pss rguments reference, use pointers void swp(int, int ) t = ; = ; = t; int =, = ; swp(, ); printf( %d %d\n,,); void swp(int *, int t = *; * = *; * = t; int =, = ; swp(&, &); printf( %d %d\n,,); *) Formtted Input: scnf Emple doule v; scnf( %lf, &v ); int d, month, er; scnf( %d/%d/%d, &month, &d, &er); 2 24
Pointers nd Arrs Pointers cn wlk long rrs int [10], *p, ; p = &[0]; /* p gets the ddress of [0] */ = *p; /* gets [0] */ = *(p+1); /* gets [1] */ p = p + 1; /* p points to [1] */ p++; /* p points to [2] */ p 25 26 Pointers nd Arrs Pointers cn wlk long rrs int [10], *p, ; p = &[0]; /* p gets the ddress of [0] */ = *p; /* gets [0] */ = *(p+1); /* gets [1] */ p = p + 1; /* p points to [1] */ p++; /* p points to [2] */ Pointers nd Arrs Pointers cn wlk long rrs int [10], *p, ; p = &[0]; /* p gets the ddress of [0] */ = *p; /* gets [0] */ = *(p+1); /* gets [1] */ p = p + 1; /* p points to [1] */ p++; /* p points to [2] */ p p 2 28
Pointers nd Arrs, cont d Arr nmes re constnt pointers int [10], *p, i; p = ; /* p points to [0] */ = p; /* Illegl; cn t chnge constnt */ ++; /* Illegl; cn t chnge constnt */ p++; /* Legl; p is vrile */ Suscripting is defined in terms of pointers [i], *(+i), i[] /* Legl nd the sme */ &[i], +i /* Legl nd the sme */ p = &[0] /* &*(+0) &* */ Pointers cn wlk rrs efficientl p = ; for (i = 0; i < 10; i++) printf( %d\n, *p++ ); 29 Pointer Arithmetic n o w i s t h e t i m e \0 1 0 0 0 2 0 0 0 0 0 0 4 0 0 0 long *p; Pointer rithmetic tkes into ccount the stride (size of) the vlue pointed to long *p; p += i; /* increments p i elements */ p -= i; /* decrements p i elements */ p++; /* increments p 1 element */ p--; /* decrements p 1 element */ If p nd q re pointers to sme tpe T p q /* numer of elements (longs, or chrs) etween p nd q */ Does it mke sense to dd two pointers? chr *p; 0 Pointer Arithmetic, cont d Pointer & Arr Prmeters Comprison opertions for pointers <, >, <=, >=, ==,!= if (p < q)... ; p nd q must point to the sme rr no runtime checks to ensure this An emple int strlen(chr *s) chr *p; for (p = s; *p; p++) ; return p s; n o w i s t h e t i m e \0 Formls re not constnt; the re vriles Pssing n rr psses pointer to 1st element Arrs (nd onl rrs) re pssed reference Declrtion: void f(int [])... is equivlent to void f(int *)... = +1; Cll: int [10];... f(); 1 2 s p p
Pointers & Strings An Emple: String Cop A C string is n rr of chr with NULL t the end n o w i s t h e t i m e \0 String constnts denote constnt pointers to ctul chrs chr *msg = now is the time ; chr msg[] = now is the time ; chr *msg = msg; /* msg points to 1st chrcter of now is the time */ Strings cn e used whenever rrs of chrs re used sttic chr digits[] = 01245689 ; putchr( 01245689 [i]); putchr(digits[i]); Pointers nd rrs re essentill the sme thing => Pointers to chrs nd rrs of chrs re the sme Arr version void scop(chr s[], chr t[]) int i = 0; while ((s[i] = t[i])!= \0 ) i++; Pointer version void scop(chr *s, chr *t) while (*s = *t) s++; t++; Idiomtic version void scop(chr s[], chr t[]) while (*s++ = *t++) ; 4 Arrs of Pointers Arrs of Pointers, cont d Used to uild tulr structures Declre rr of pointers to strings chr *line[100]; chr *(line[100]); /* sme s ove */ chr (*line)[100]; /* never used */ Initiliztion emple chr *month(int n) sttic chr *nme[] = Jnur, Ferur, Mrch, April, M, June, Jul, August, Septemer, Octoer, Novemer, Decemer ; Reference emples line[i] /* refers to the i-th string */ *line[i] /* refers to the 0-th chr of the i-th string */ ssert(n >= 1 && n <= 12); return nme[n-1]; Another emple int, ; int *[] = &, &, &, &, NULL; 5 6
Arrs of Pointers, cont d An rr of pointers is 2-D rr int [10][10]; int *[10]; Arr : 2-dimensionl 1010 rr Storge for 100 elements llocted t compile time Ech row of hs 10 elements, cnnot chnge t runtime [6] is constnt 2-d rrs re stored in row-mjor order row 2 row 1 row 0 Arr : An rr of 10 pointers; ech element could point to n rr Storge for 10 pointers llocted t compile time Vlues of these pointers must e initilized t runtime Ech row of cn hve different length (rgged rr) [6] is vrile; [i] cn chnge t runtime More Emples Equivlence emple void f(int *[10]); /* known numer of rows */ void f(int **); Another equivlnce emple void g(int [][10]); /* known numer of columns */ void g(int (*)[10]); Legl in oth f nd g: ** = 1; 8 Commnd-Line Arguments Pointers to Functions B convention, min() is clled with 2 rguments int min(int rgc, chr *rgv[]) rgc is the numer of rguments, including the progrm nme rgv is n rr of pointers to the rguments Emple: % echo hello rgc = 2 rgv[0] = echo rgv[1] = hello rgv[2] = NULL Implementtion of echo min(int rgc, chr *rgv[]) int i; for (i = 1; i < rgc; i++) printf( %s%c,rgv[i], (i < rgc-1)? : \n ); eit(0); 9 #include <stdio.h> int dd(int, int ) return + ; int mul(int, int ) return * ; min() int [5] = 1, 2,, 4, 5; int sum = doarr(, 5, 0, dd); int prod = doarr(, 5, 1, mul); printf("sum = %d, product = %d\n", sum, prod); int doarr(int [], int n, int vl, int (*op)(int, int)) int i; for (i = 0; i < n; i++) vl = (*op)(vl, [i]); /* vl = op(vl, [i]); */ return vl; 40
Pointers to Functions, cont d Declrtion snt cn e confusing: int (*op)(int, int) declres op to e pointer to function tht tkes two int rguments nd returns n int int *op(int, int) declres op to e function tht tkes two int rguments nd returns pointer to n int Invoction snt cn lso confuse: (*op)(, ) clls the function pointed to op with the rguments nd, equivlent to op(, ) *op(, ) clls the function op with rguments nd, then dereferences the vlue returned Pointers to Functions, cont d A function nme itself is constnt pointer to function (like n rr nme) int dd(int, int ) int mul(int, int ) int sum = doarr(, 5, 0, dd); int prod = doarr(, 5, 1, mul); Function cll hs higher precedence thn dereferencing 41 42 Pointers to Functions, cont d Arrs of pointers to functions etern int mul(int, int); etern int dd(int, int);... int (*opertors[])(int, int) = mul, dd,... ; To invoke (*opertors[i])(, ); Summr Pointers tpe * (int *p) declres pointer vrile * nd & re the ke opertions Opertion rules Unr opertions ind more tightl thn inr ones Pointer rithmetic opertions consider size of the elements Pointers nd rrs hve tight reltionship An rr is constnt pointer pointing to the 1 st element A pointer cn wlk through elements of n rr An rr of pointers is 2-D rr (1-D fied nd nother vrile) Mster how to get commnd-line rguments from min() 4 Pointers to functions Cn e used to prmeterize functions 44