1 / 11

Chapter 6

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

bastien
Download Presentation

Chapter 6

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. Chapter 6 Pointers: Getting a Handle on Data

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

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

  4. & 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

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

  6. 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)++; }

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

  8. 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])); } } }

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

  10. 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]; }

  11. 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 } }

More Related