Vous êtes sur la page 1sur 9

Pointer Variables

A pointer is the memory address of a variable. The address points to the variable because it defines where the variable is in the memory. A pointer can be stored in a variable. So, pointer variables are used to store addresses rather than integers or real numbers. Pointer variables can be used to link classes and objects. Also used well in creating data structures such as stacks, queues and linked lists. The following program calculates the volume of a cube using both ordinary variables and pointer variables. Source code
#include <iostream> using namespace std; int main( ) { double double* double* double* Declaring three pointer width=5.2, variables: height_address, depth=3.1; width_address, and depth_address. The * is needed to indicate that addresses are stored in these variables memory cells.

volume,

height=10.5,

height_address; width_address; depth_address;

height_address width_address depth_address

= &height; = &width; Assigning addresses to thepointer variables. The = &depth; operator is used to specify address of &

height, address of width, address of depth volume = height * width * depth; We can calculate the volume using the cout<<"Volume = "<<volume<<endl; ordinary variables. volume = (*height_address) * (*width_address) * (*depth_address); cout<<"Volume = "<<volume<<endl; We can also calculate the cout <<"&volume = "<< &volume<<endl volume using the addresses of <<"&height = "<< &height<<endl the oridinary variables. The * <<"&width = "<< &width<<endl operator, used just before the <<"&depth = "<< &depth<<endl pointer variables, means get <<"height_address = "<< height_address<<endl the value <<"width_address = "<< width_address<<endl at . Here we get the values <<"depth_address = "<< depth_address<<endl at height_address, <<"&height_address = "<< &height_address<<endl width_address, <<"&width_address = "<< &width_address<<endl depth_address <<"&depth_address = "<< &depth_address<<endl; } 34

Output

Both methods gives the same result Volume = 169.26 Volume = 169.26 &volume = 0012FF78 &height = 0012FF70 Note that the values of height_address, &width = 0012FF68 &depth = 0012FF60 width_address, and depth_address match height_address = 0012FF70 &height, &width, and &depth, respectively. width_address = 0012FF68 depth_address = 0012FF60 &height_address = 0012FF5C Note that, the addresses in this program may &width_address = 0012FF58 be changed when the program runs again on &depth_address = 0012FF54 another computer Press any key to continue

Pointer operators:

Ampersand & Asterisk *

means address of

means get value at the address type* pointer _variable;

Declaring a pointer variable:

where type can be int or double, and pointer-variable is a programmer chosen name. for example: double* height_address;

This declares that height_address is a pointer variable and at the address stored in it there exist a double value of another normal variable. For multiple pointer declarations OR double *height , *width;

double* height; double* width; Assigning an address to a pointer variable: We use the address of operator (&) pointer_variable = &ordinary_variable ; for example: height_address = &height;

using a pointer variable: A pointer variable can be used to access a value. We simply go to the address stored in the pointer variable and get the value.
35

For example : *height_address gets the value stored at the address held by the pointer variable height_address (Which is 0012FF70) and the value is 10.5

Pointer variables and Functions:


Declaring a function with three pointer variables (type double*) as arguments. This means these arguments hold addresses. The function returns a #include <iostream> using namespace std; double Calling the function to calculate double calc_volume(double*, double*, double*); volume. Here, the height, the width, and depth addresses int main( ) (&height, &width, and { &depth)are passed to the pointer double volume, height=10.5, width=5.2, depth=3.1; variables (height_address, volume = height * width * depth; width_address, and cout<<"Volume = "<<volume<<endl; depth_address) Source Code volume = calc_volume(&height, &width, &depth); cout<<"Volume = "<<volume<<endl; } double calc_volume (double* height_address, double* width_address, double* depth_address) { double volume; volume = (*height_address) * (*width_address) * (*depth_address); return (volume); }

Output Volume = 169.26 Volume = 169.26

36

One-Dimensional Numeric Arrays and Functions: For large arrays, if we were to pass each value to a function we would use a large amount of memeory. Rather than passing array values to a function, we pass the address of the first element of an array (will refered as the address of the array) . In Function declaration the array address consists of a data type and [ ] . For example, double [ ] means address of an array of double values int [ ] means the address of an array of int values In Function call, the array address consists of the array name with no brackets. For example, x means the address of array x[ ] In Function header, the array address consists of the data type, name, and empty brackets. For example, double a[ ] means that within the function we use a[0] to reperesent the first element of an array of double, and a[1] to represent second element, .. Source code
This indicates that the first element passed to function2 is the address of a 1-D array of double #include <iostream> values using namespace std; The const means that even though function3 void function2 (double[ ], int); receives the address of array, it cannot use the address void function3 (const double[ ], int); the contents of the array to modify int main ( ) { The function arguments, c without brackets, double c[5]={2.,4.,6.,8.,10.}; represent the address of the first element of the c[ ] array. The second arg, 5, represents the number of elements of the c[ ] array. function2(c,5); function3(c,5); } In the header, we use double b[ ] not just double [ ].

Since the void function2 (double b[ ] , int num_elem) address of c was passed to b , b[0] { represents c[0], and b[1] represents c[1] and int i; for (i=0; i<num_elem; i++) b[i] *=10.; This loop modifies each element of the c[ ] } array void function3 (const double d[ ] , int num_elem) 37 In function3, the const for d[ ] in the { int i; header means that we cannot modify the cout<<"c[ ] is "; array contents. for (i=0; i<num_elem ; i++) cout<< d[i] << " "; }

Output
c[ ] is 20 40 60 80 100 Press any key to continue

Note that : The memory region for function2 does not contain an array. The actual element values are contained in main memory region. Only the address of the beginning of that region transferred from c to b C++ also allows the pointer notation * to be used in place of brackets. For example: Declaration void function2 (double[ ], int); will be void function2 (double* , int); Call will be the same function2(c,5); function2(c,5);

Header void function2 (double b[ ] , int num_elem) will be void function2 (double* b , int num_elem) Note that using double* indicates that b represent an address

Pointer variables with Arrays and Functions: Source Code We can use pointer variables to transfer array information to a function as in the #include <iostream> followingnamespace std; using program
void function2 (double*, int); In function2 argument list, double* is used to indicate that the first argument is an address

int main ( ) { double c[5]={2.,4.,6.,8.,10.}; function2(c,5); } Declaring and initializing a 1-D array void function2 (double* b, int num_elem) { int i; The function2 argument c without barackets, for (i=0; i<num_elem; i++) represents the address of the first element of 38 { the c[ ] array b[i] *=10.; cout<<b[i]<<endl; } }

In the header, b is declared to be a pointer variable. It is used to hold the address of the beginning of the array c[ ] Even though b is a pointer variable, C++ allows the use of array notation (as shown by b[i]) to access memory cells located beyond the one stored by b.

Output
20 40 60 80 100 Press any key to continue

Addition and subtraction with Pointer variables: Source code


#include <iostream> using namespace std; int main ( ) { double a[3]={10, 11, 12}; double* b; b=&a[0]; cout<<"b="<<b <<endl; cout<<"b+1="<<b+1<<endl; cout<<"b[0]="<<b[0]<<" b[1]="<<b[1]<<" b[2]="<<b[2]<<endl; b=&a[1]; cout<<"b[1]="<<b[1]<<endl; b[1]=5.4; cout<<"a[2]="<<a[2]<<endl; } 39

Output
b=0012FF68 b+1=0012FF70 b[0]=10 b[1]=11 b[2]=12 b[1]=12 a[2]=5.4 Press any key to continue

Dynamic Memory Allocation (Dynamic arrays and pointers): Programming with arrays can wast considerable memory because the programmer sets the size of an array, but a user fills it. C++ has the new and delete operators which enable us to reserve and unreserved memory for arrays while a program is executing. Pointer variables are used with new and delete because these operators work with addresses of the memory reserved. In the following program we creat an array of pass codes for a group of students, one code for each student. The user enter the number of students and memory is reserved for the pass code array using the new operator. The array is filled with random numbers in the range 1000 to 9999. #include <iostream>
#include <ctime> Note that there is no explicit declaration for an array. We declare only a pointer using variable. namespace std; int main ( ) { int i, num; int* pass_codes; The cstdlib header is needed for functions rand() and srand() and operators new and delete cout <<"Enter the number of students."<<endl; cin >>num; The ctime header is needed for the time() function cout <<"The pass codes are:"<<endl; pass_codes = new int[num]; srand(time(0)); The new operator reserves memory for (i=0; i<num; i++) for num integers. The new operator { pass_codes[i] = rand() % 9000 +returns the address of the beginning of 1000; this memory. The address is assigned cout<<pass_codes[i]<<" "; } to the pass_code pointer. cout<<endl; 40 cout<<"The array address = "<<pass_codes<<endl; delete [ ] pass_codes; } #include <cstdlib>

time() function returns the time of day in seconds from midnight. The srand() function seeds the rand() function rand()% 9000 + 1000 function produces a random number in the range 1000 to 9999 Delete operator unreserve all the memory reserved at the address indicted by pointer pass_code

Enter the number of students. 10 The pass codes are: 1113 4136 9118 1358 7849 5516 7931 7750 5301 7873 The array address = 00491C90 Press any key to continue

Enter the number of students. 10 The pass codes are: 1365 3563 8403 1225 6639 3962 3446 2726 7742 7992 The array address = 00491C90 Press any key to continue

Program Description: The new operator: the form for using this operator to reserve memory for an array is new data-type [num-elements] example: new int [num]; reserves memory for an array of int values with the number of elements equal num Note that the user determine the number of elements not the programmer.

41

The delete operator: It is good to release memory prior the end of program execuation using the delete operator. Its form is delete [ ] array_address ; example: delete [ ] pass_codes ; this releases (free) the memory beginning from the address indicated by pass_code till the size reserved by new (all the array) Note: generating random number within a specified range

For any integer n and using the mod operator % so n%25 is number from 0 to 24 Also n%6 +1 is a number from 1 to 6. So to creat a number from minimum 1000 to maximum 9999. The range size is 9999 1000 = 8999. The random number in the range min to max is computed from (n%range size) +min rand()% 9000 + 1000 function produces a random number in the
range 1000 to 9999

42

Vous aimerez peut-être aussi