Pointers Pointers What is pointer A variable 2
Pointers
Pointers • What is pointer? • A variable (2 or 4 bytes long) that can hold an Address • Why pointers? • Sometimes the only way to express a computation • Usually lead to compact and efficient code • Related operators: - * : can be used in two ways • In declaration : read as pointer, e. g. , int *p; • In accessing : read as content of, e. g. , x = *p; - & : returns LValue (address) of a variable • Read as address of 2
Pointer Examples int x = 70, y = 80, z[4] = {10, 20, 30, 40 }; int *ip; // int pointer ip ip = &x; *ip = 200; // ip is assigned to address of x // content of ip is assigned to 200 y = *ip; ip = &z[2]; *ip = *ip + 20; y = *ip+1; // y is assigned to content of ip // same as *ip += 20; x : 4892 200 70 y : 4894 200 51 80 Z, Z[0] : 4896 10 Z[1] : 4898 20 Z[2] : 4900 30 50 Z[3] : 4902 40 ip : 4904 ? ? 4892 4900 3
Pointer Examples int x = 100; int *p 1 = &x; cout << "&x == " << &x << "p 1 == " << p 1 << " x == " << x << endl *p 1 == " << *p 1 << endl; *p 1 += 20; cout << "&x == " << &x << "p 1 == " << p 1 << " x == " << x << endl *p 1 == " << *p 1 << endl; Output &x == 0 x 7 fff 7 e 60 f 41 c x == 100 p 1 == 0 x 7 fff 7 e 60 f 41 c *p 1 == 100 &x == 0 x 7 fff 7 e 60 f 41 c x == 120 p 1 == 0 x 7 fff 7 e 60 f 41 c *p 1 == 120 4
Pointers in Function arguments • Arguments are passed by value, thus only a copy is passed. void swap_wrong(int x, int y) { int temp; temp = x; x = y; y = temp; } void swap(int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; } int a = 10, b = 20; swap_wrong(a, b); cout << a << b; swap(&a, &b); cout << a << b; in swap_wrong in swap x: 10 px: 4397 y: 20 py: 9643 temp: in caller: temp: a: 4397 10 b: 9643 20 5
Referencing & Dereferencing pointer & variable * [] • Referencing ( & ): create reference (pointer) to memory location • Dereferencing (*, [ ] ): get the content of memory location Declaration Reference/Pointer/ Address/LValue Dereference/Variable/ Content/RValue int x; &x x int z[10], i; z z+i &z[i] z[0] *(z+i) z[i] int *ptr, i; ptr+i &ptr[i] *ptr or ptr[0] *(ptr+i) ptr[i] Note: x = 10; *(&x) = 10; 6
Pointer Operations • The valid pointer operations are: 1. Adding or subtracting a pointer and an integer (p i) 2. Assignment of pointers of the same type (p = q) 3. Subtracting or comparing two pointers in same array (p q) 4. Assigning or comparing to zero. (p = 0) • All other pointer arithmetic are illegal. 7
1– Addition/Subtraction with int • Used for pointing to different elements in an array int a[6], *pa, x, i; pa = &a[2]; // same as pa = a + 2; *pa pa[0] a[2] *(pa-i) pa[-i] a[2 -i] *(pa+i) pa[i] a[2+i] pa-2 pa-1 pa: pa+1 pa+2 pa+3 a: a[0] a[1] a[2] a[3] a[4] a[5] pointer int pointer 8
Addition/Subtraction with int • Size of the pointed data-type is automatically taken care of. • If address of a pointer (say ptr) is A then address of ptr+i is A+i*sizeof(data-type of ptr) long *lp; int *ip; char *cp; 7832 12 7833 34 7834 56 7835 44 cp 7832 *cp=12 7832 12 7833 34 7834 56 7835 44 7836 33 cp+2 7834 cp[2]=56 7837 56 ip 7832 *ip=3412 ip+1 7834 ip[1]=4456 ip+2 7836 ip[2]=5633 7832 12 7833 34 7834 56 7835 78 7836 78 7837 56 7838 34 7839 12 lp 7832 *lp= 78563412 lp+1 7836 lp[1]= 12345678 9
Assignment of Pointers • Pointers of same types can be assigned. float x = 10. 5, *p = &x, *q = p; • Pointer to void • is used to hold any type of pointer int *ip; void *vp; vp = ip; // type casting is not essential • Cannot be de-referenced without type casting. int x = *vp; // is illegal type casting is a must int y = *((int *)vp); // is ok 10
Comparing/Subtraction to Pointer • The resulting type of subtracting two pointers is integer. • Comparison Vs. Subtraction • a<b a–b<0 • a == b a – b == 0 • a>b a–b>0 • Compared/Subtracted pointers should point in same array. • Example: /* strlen: return length of string s*/ int strlen(char *s) { char *p = s; while (*p != ‘ ’) p++; return p – s; pointer - pointer int } 11
Comparing/Assigning to Zero • Zero is the only integer that can be assigned to a pointer • Zero is the only integer that a pointer can be compared to. • Use this to initialize a pointer and to check validity char *m = 0; . . . if (m != 0) {. . . safe to use the pointer. . . } • NULL can be used instead of 0 12