110 likes | 223 Views
Chapter 6. Pointers: Getting a Handle on Data. What is a pointer?. A pointer is a variable that contains an address Example: integer a is associated with location 1000 int a = 5 integer b is associated with location 1004 int b = 3 integer c is associated with location 1008
E N D
Chapter 6 Pointers: Getting a Handle on Data
What is a pointer? • A pointer is a variable that contains an address • Example: • integer a is associated with location 1000 • int a = 5 • integer b is associated with location 1004 • int b = 3 • integer c is associated with location 1008 • int c = 8 • p contains pointer to an integer • int*p = &a; // p = 1000, int* means pointer to an integer //&a means the address of a
Declaring a pointer • General syntax: • type* p; • Example: • int *p; // pointer to an integer • Getting the pointer of a variable(using &) • int a = 3; • int *p = &a; • Using pointers • p++; //increments p to point to next integer • (*p)++; //increments the value of a by one
& and * • & is the address operator • int i = 3; • cout << &i; //prints the address of variable i • * is the dereference operator (opposite of pointer) • int i = 3; • int* j = &i; // now *j and i are aliases • (*j)++; • cout << i; //prints 4
Pointers and functions • By default, functions cannot modify input parameter • Example: int main(){ int i=3; inc(i); cout << i; // 3 is printed } void inc(int i){ i++; } only the local variable i in the method inc is changed the variable i in the main method is not changed
Pass by pointer • If you send a pointer to a variable to a function, then the function can modify the variable (the value of the pointer is not changed). int main(){ int i=3; inc(&i); cout << i; //4 is printed } void inc(int *i){ (*i)++; }
The swap function swap(int *i, int *j){ int temp = *i; *i = *j; *j = temp; } use: int main(){ int a[]={2,324,2,3,2,2}; swap(&(a[0]),&(a[3])); cout << a[0] << " " << a[3]; }
Bubble sort int a[]={241,234,21,45,6666,34,3}; int size = 7; for(int n=size-1; n>=1;n--){ for(int i=0; i < n; i++){ if(a[i] > a[i+1]){ swap(&(a[i]),&(a[i+1])); } } }
Pointer arithmetic • Example: int a[] = {4,3,2,66,2}; // a is a pointer int *p = a; // p is a second pointer to the same array p = a+2; // now p points to the third element of the array, actual value incremented by 8 • In general, newAdress = oldAdress+ i means: • newAddress = oldAddress + i *(size of type); • For example: • p++; means move to the next element of the array
Example #include <iostream> using namespace std; void main(){ inta[10]; for(int i=0; i < 10; i++){ a[i]=i; } int*p = a; p++; cout<< p[0]; }
Pointers and Array processing • int a[5]; defined a to be a pointer to an array of 5 elements • a++; advances the pointer by one • *(a+i); refers to the ith element of the array a • Code to initialize an array to all zeros void fillWithZeros(int a[], int size){ while(size-- > 0) *(a++)=0; } equivalent to void fillWithZeros(int a[], int size){ while(size > 0){ size--; *(a)=0; //sets the current element to 0 a++; // moves to the next element of the array } }