Address Operator
int ptr, num=45;
ptr = #
This places the address where num is stored into the variable ptr. If num is stored in memory 21260 address, then the variable ptr has the value 21260.
Pointer Expressions and Pointer Arithmetic
Pointer are variables. They are not integer, but they can usually be displayed as unsigned integer. The conversion specified for a pointer a pointer is added and subtracted. For example, ptr++ causes the pointer to be incremented, but not by 1. Suppose n is an integer and the system stores integer and the system stores integers in two bytes. Further suppose the address of n would occupy bytes 65524 and 65523.
Example:
int n, *p;
n=16;
p=&n;
p++;
cout<
Example: if ptr1 and ptr2 are declared as pointer variables, then the following are valid use of pointers in arithmetic operators.
1. *ptr1 = *ptr + *ptr; Same as (*ptr1)+(*ptr2)
2. sum = *ptr +10; Same as (*ptr) + 10
3. sum += *ptr2; Same as sum + (*ptr2)
4. result = 10 * - *ptr1/*ptr; Same as ((10 * (-(*ptr1))) / (*ptr2))
5 ptr1 > ptr2;
6. ptr2 != ptr2;
Pointers and Functions
The usage of the pointers in a function may be classified into two group:
1. Call by Value.
2. Call by Reference.
Call by Value - We have seen that when a function is invoked, there is established a correspondence between the formal and actual parameters. A temporary storage is created where the value of the actual parameter is stored. The formal parameter picks up its value from this storage area.
Example:
#include
void main()
{
int a=20, b=30;
Value of a and b before calling the function:
a = 20
b = 30
Value of a and b after the function called:
a = 20
b = 30
Call by Reference - The process of calling a function using pointers to pass the address of variable is known as call by reference. The function which is called by 'reference' can change the value of the variable used in the call.
Example:
#include
int a=100;
cout<<"Value of a before calling the function = "<<<"Value
Pointers and Arrays
Example:
#include
void main()
int i=3, *x;
float j=1.5, *y;
char k='c', *z;
Value of i = 3
Value of j = 1.5
Value of k = c
Original Address in x = 0x8f5efff4
Original Address in y = 0x8f5efff0
Original Address in z = c
New Address in x = 0x8f5efff6
New Address in y = 0x8f5efff4
New Address in z =
Example:
#include
void main()
int arr[] = {10,20,30,40,50,60,70};
int *i, *j;
i=&arr[1];
j=&arr[5];
cout<<"\nThe Address of arr[1] = "<<&arr[1
]; cout<<"\nThe Address of arr[5] = "<<&arr[5]; cout<<"\nThe Address of i = "<<&i; cout<<"\nThe Address of j = "<<&j; cout<<"\nj - i = "<
The Address of arr[1] = 0x8fc9ffe6
The Address of arr[5] = 0x8fc9ffee
The Address of i = 0x8fc9fff4
The Address of j = 0x8fc9fff2
j - i = 0x8fc9ffee - 0x8fc9ffe6 = 4
*j - *i = 60 - 20 = 40
Example: Here is a program that prints out the memory locations in which the elements of this array are stored.
void main()
{
int num[]={24,34,12,44,56,17};
for(int i=0; i<=5; i++){ cout<<"\nElement No. "<<<" Address = "<<&num[i]; } }
Element No. 0 Address = 0x8fbfffea
Element No. 1 Address = 0x8fbfffec
Element No. 2 Address = 0x8fbfffee
Element No. 3 Address = 0x8fbffff0
Element No. 4 Address = 0x8fbffff2
Element No. 5 Address = 0x8fbffff4
#include
void main()
{1212, 33},
{1434, 80},
1234 56
1212 33
1434 80
1312 78
#include
{
int *arr[4]; //array of integer pointers
int i=31, j=5, k=19, l=71;
arr[0]=&i;
arr[1]=&j;
arr[2]=&k;
for(int m=0; m<=3; m++){ cout<<"\nThe Value of "<<(char) (73+m)<<" is "<<*arr[m]; cout<<"\nThe Carrying Value of arr["<
The Value of I is 31
The Carrying Value of arr[0] is 0x8fb0ffec
The Address of arr[0] is 0x8fb0ffee
The Value of arr[0] is 31
The Value of J is 5
The Carrying Value of arr[1] is 0x8fb0ffea
The Address of arr[1] is 0x8fb0fff0
The Value of arr[1] is 5
The Value of K is 19
The Carrying Value of arr[2] is 0x8fb0ffe8
The Address of arr[2] is 0x8fb0fff2
The Value of arr[2] is 19
The Value of L is 71
The Carrying Value of arr[3] is 0x8fb0ffe6
The Address of arr[3] is 0x8fb0fff4
The Value of arr[3] is 71
Example: The following program would justify an array of pointers can have the address of other arrays.
#include
void main()
{
int *ptr[]={a, a+1, a+2, a+3, a+4};
for(int i=0; i<=4; i++) cout<<'\n'<<*ptr[i]<<" Stored at Address "<
1 Stored at Address 0x8fcaffec
2 Stored at Address 0x8fcaffee
3 Stored at Address 0x8fcafff0
4 Stored at Address 0x8fcafff2
5 Stored at Address 0x8fcafff4
Pointers to Pointers
The concept of pointer are further extended. Pointer we know is a variable which contains the address of a variable.
Example:
#include
{
int a=45;
int *ptr;
int **ptr_ptr;
ptr=&a;
ptr_ptr=&ptr;
cout<<"\nAddress of a = "<<&a; cout<<"\nAddress of a = "<
Address of a = 0x8faafff4
Address of a = 0x8faafff4
Address of a = 0x8faafff4
Address of ptr = 0x8faafff2
Address of ptr = 0x8faafff2
Address of ptr_ptr = 0x8faafff0
Value of a = 45
Value of a = 45
Value of a = 45
Value of a = 45
Value of ptr = 0x8faafff4
Value of ptr_ptr = 0x8faafff2