Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.

Similar presentations


Presentation on theme: "Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not."— Presentation transcript:

1 Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not the same as the variable whose address is being stored; a pointer to int is not type int. You might think a pointer data type would be called something like pointer or ptr. Some common uses of pointers: Accessing array elements Passing arguments by reference to a function ( updatable arguments) Passing arrays and strings to functions Obtaining memory from the system Creating data structures such as linked lists

2 Addresses and Pointers  Every byte in the computer’s memory has an address.  Addresses are numbers, just as they are for houses on a street. The numbers start at 0 and go up from there—1, 2, 3, and so on.  If you have 1MB of memory, the highest address is 1,048,575.  When a program is loaded into memory, it occupies a certain range of these addresses.  That every variable and every function in the program starts at a particular address.  You can find the address occupied by a variable by using the address-of operator &.

3 Memory addresses.

4 The Address-of Operator & // Dealing with addresses of variables #include int main() { int var1 = 11; //define and initialize int var2 = 22; //three variables int var3 = 33; cout << &var1 << endl //print the addresses << &var2 << endl //of these variables << &var3 << endl; return 0; } 0x23ff74 0x23ff70 0x23ff6c Note: The << insertion operator interprets the addresses in hexadecimal values.

5 Addresses and contents of variables.

6 Pointer Variables Pointers are variables that hold address values. The syntax used in C++ allows pointers to any type to be declared.  Examples of pointer declarations: char* cptr; // pointer to char Int * iptr; // pointer to int Float * fptr; // pointer to float char* ptr1, * ptr2, * ptr3; // three variables of type char*

7 A Simple Program Using Function Declaration

8 Accessing the Variable Pointed To Remember that the asterisk used as the dereference operator has a different meaning than the asterisk used to declare pointer variables. The dereference operator precedes the variable and means value of the variable pointed to by. The asterisk used in a declaration means pointer to. Examples: int* ptr; //declaration: pointer to int *ptr = 37; //indirection: value of variable pointed to by ptr int v; //defines variable v of type int int* p; //defines p as a pointer to int p = &v; //assigns address of variable v to pointer p v = 3; //assigns 3 to v *p = 3; //dereferencing: also assigns 3 to v

9 // pointers (address variables) #include int main() { int var1 = 11, var2 = 22 ; //two integer variables cout<<"\n print addresses of variables using address operator &"<<endl; cout << “\t”<<&var1 <<“\t” << &var2 << endl; cout<<"\n print addresses of variables using a pointer variable ptr "<<endl; int* ptr1; //pointer to integers ptr1 = &var1; //pointer points to var1 cout << ptr1 << endl; //print pointer value ptr = &var2; //pointer points to var2 cout << ptr << endl; //print pointer value cout<<"\n print the values of variables using a pointer variable *ptr "<<endl; ptr = &var1; //pointer points to var1 cout << *ptr << endl; //print the int value that the pointer points to ptr = &var2; //pointer points to var2 cout << *ptr << endl; //print the int value that the pointer points to return 0; }

10 Variable Access via pointer.

11 // pointers (address variables) #include int main() { int var1 = 122, var2 = 435 ; //two integer variables cout<<"\n The addresses of variables using address operator &"<<endl; cout << "\t"<<&var1 <<"\t" << &var2 << endl; cout<<"\n The addresses of variables using pointers ptr1 and ptr2 "<<endl; int* ptr1, * ptr2; //pointer to integers ptr1 = &var1; //pointer points to var1 ptr2 = &var2; //pointer points to var2 cout <<"\t"<< ptr1 <<"\t"<<ptr2 << endl; //print pointers values cout<<"\n The values of variables pointed by ptr1 and ptr2 "<<endl; cout <<"\t"<< *ptr1 <<"\t"<<*ptr2 << endl; //print pointers values return 0; }

12 Pointers and Arrays Array elements can be accessed using pointer notation as well as array notation. The name of an array is its starting address, for example: int intarray[5];//this means intarray has the address of that array cout<< * ( intarray+3 ); //this prints the fourth element in the array intarray The C++ compiler takes the size of the data type into account when it performs data addresses. It knows that intarray is an array of type int because it was declared that way. So when it sees the expression intarray+3, it interprets it as the address of the fourth integer in intarray, not the fourth byte.

13 Pointers and Arrays

14 ` // array accessed with pointer notation #include using namespace std; int main() { int intarray[5] = { 31, 54, 77, 52, 93 }; //array for(int j=0; j<5; j++) // for each element, cout << *(intarray+j) << endl; // print value return 0; }


Download ppt "Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not."

Similar presentations


Ads by Google