Académique Documents
Professionnel Documents
Culture Documents
Data Type of the variable Actual Value Stored in the Variable The Address of the Variable
Introduction to Pointers
Introduction to Pointers
Introduction to Pointers
int *p Means a location P which can contain address of an integer In other word P can point towards an integer variable (hence given the name Pointer)
Declaration of Pointers
datatype *X X is name of Pointer Datatype is the type of type to which X will point Each variable that is declared as pointer must be preceded by an *
Example
int x=14, *P; //P is a pointer
P ? x 14
Normally a variable contains a specific value Pointers contain address of variable So a variable name directly reference a value and a pointer indirectly references a value
Dereference Operator
To get the value stored at the memory address we use dereference operator(*)
* is used with the name of the pointer to get the value stored at the address
Note that address of a and value of ptr are identical & and * are inverses of each other----when they are applied to ptr, consecutively (in any order) the same result is printed.
Initialization of Pointers
Pointers should be initialized either when they are declared or in an assignment statement A pointer may be initialized to 0, NULL or to a address Pointer initialized with 0 or NULL points to nothing
Arrays are always passed by Reference Arrays are not passed using & Name of the array is the address of starting location in the memory of the array The name of array is equivalent to &Array[0]
Pointers are valid operands in arithmetic expressions, assignment expressions and comparison expressions However not all the operators used in these expressions are valid for pointer variables A limited set of arithmetic operations may be performed on pointers
A pointer may be incremented or decremented (++ and --) An integer may be added to a pointer(+ or +=) An integer may be subtracted from a pointer(- or -=) One pointer may be subtracted from another
Arrays are always passed by Reference Arrays are not passed using & Name of the array is the starting location in the memory of the array The name of array is equivalent to &Array[0]
Pointers are valid operands in arithmetic expressions, assignment expressions and comparison expressions However not all the operators used in these expressions are valid for pointer variables A limited set of arithmetic operations may be performed on pointers
A pointer may be incremented or decremented (++ and --) An integer may be added to a pointer(+ or +=) An integer may be subtracted from a pointer(- or -=) One pointer may be subtracted from another
Pointer Arithmetic
int *y = &x; y = y+1;
The y = y+1 will set y to the address of the next integer after x If x has address 0, then it uses bytes 0, 1, 2, 3. Thus y gets 0, and then changes to 4 (not 1)
int y[10]
This means that we have reserved memory spaces for ten integers and named it collectively as y Y represents the memory address of the beginning of this collective memory space
int y[10]
The first element of array can be accessed as y[0] Memory address of first element i.e. y[0] is stored in y
The name of array is a constant pointer which contains the memory address of the first element of the array The difference between this and an ordinary pointer is that the array name is a constant pointer
The difference between this and an ordinary pointer is that the array name is a constant pointer It means that array name will always point to start of the array It can not be reassigned to any other address
Example
int y[10]; int *yptr; yptr=y; Now we have two things pointing to the same place (y and yptr) Both are pointing towards first element of the array
Example
int y[10],x; int *yptr=&x; y=yptr;
What is an array?
each of the 10 integers In truth, arr is of type int*, with the same address as arr[0]
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15 16 17 18 19
Example
int main() { int arr[20]; int i; for (i=0; i < 20; i++) {
*(arr+i) = 20-i; //Assigns values to the array
} for (i=0; i < 20; i++) { cout << arr[i]; // Prints the values }
Explanation
The example prints all numbers from 0 to 19 In the example, we access the elements in two different ways:
In the first loop, we used pointer arithmetic In the second loop, use the [ ] operator They do the same thing we could have
used either method for either loop
Another Example
When a Pointer is incremented it actually jumps the number of memory spaces according to the data type that it points to
void*
allows us to abstract data types We will not go into the detail of what is an ADT
Warning!
You can use the expression ptr++ for a pointer variable But You cannot use the ++ operation if the pointer is the name of an array It is a constant pointer and can't be changed.
int* p = 20; *p = 0; Reset bytes 20, 21, 22 and 23, each, to 0. Who knows it could be important!! Maybe it was some other variable you are using Maybe it has something to do with the hard drive It is theoretically possible to crash the system
Declaring Array
Need
What if you need to specify array size when program is executing (Runtime)
You will also need this in your project
new
Consider
int *Ptr; Ptr = new int;
new operator
Creates variable of proper size for type int Returns pointer of specified type
int *gradesArray = new int[ 10 ];
Allocating arrays
delete
Operator delete
Deallocates memory
De-allocating arrays delete [] gradesArray;
Pointers to Pointers
We use double dereference to access the elements of a 2D array by using Array name (a pointer) to access a row (another pointer) and further to access a column element In case of single dereference, the value of pointer is the address of the variable that contains the value desired
Double Indirection
Variable Value
Earlier we used arrays and pointers interchangeably We can think a pointer to pointer is like a pointer to a group of arrays because a pointer itself can be considered as an array
Example
Suppose we have a group of character strings We can store them in a 2D array If we use conventional 2D array like Name[5][10] we can store 5 strings each of length 9 characters.
Arrays of Pointers
Array does not store strings, only pointers to strings suit array has fixed size, but can point to strings of any size
Arrays of Pointers
More Examples
void main() { char *Name[15]; int Total; cout<<"How many Names will You Enter"<<endl; cin>>Total; for(int i=0;i<Total;i++) Name[i]=new char[15]; for(i=0;i<Total;i++) { cout<<"Enter" <<i<<"th Name"<<endl; cin>>Name[i]; } for(i=0;i<Total;i++) cout<<Name[i]<<endl; }
Thank You