### Pointers

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
int function(int, int);
void main()
{
int a=20, b=30;

cout<<"\nValue of a and b before calling the function:\na = "<<<"\nb = "<<<"\nValue of a and b after the function called:\na = "<<<"\nb = "<

Pointers and Arrays

Example:
#include
void main()

{
int i=3, *x;
float j=1.5, *y;
char k='c', *z;

cout<<"\nValue of i = "<<<"\nValue of j = "<<<"\nValue of k = "<<<"\nOriginal Address in x = "<<<"\nOriginal Address in y = "<<<"\nOriginal Address in z = "<<<"\nNew Address in x = "<<<"\nNew Address in y = "<<<"\nNew Address in z = "<

Output
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

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 = "<<<" - "<<<" = "<<<"\n*j - *i = "<<*j<

<" - "<<*i<<" = "<<*j-*i; }

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

Output:
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

Example:

#include
void main()

{
int s[5][2]={
{1234, 56},
{1212, 33},
{1434, 80},

{1312, 78}
};
for(int i=0; i<=3; i++){ cout<<'\n'; for(int j=0; j<=1; j++) cout<<*(*(s+i)+j); } }

Output:
1234 56

1212 33
1434 80
1312 78

Example:

#include
void main()
{
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;

arr[3]=&l;
for(int m=0; m<=3; m++){ cout<<"\nThe Value of "<<(char) (73+m)<<" is "<<*arr[m]; cout<<"\nThe Carrying Value of arr["<<<"] is "<<<"\nThe Address of arr["<<<"] is "<<&arr[m]; cout<<"\nThe Value of arr["<<<"] is "<<*arr[m]; } }

Output:
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 a[]={1,2,3,4,5};
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 "<

Output:

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
void main()
{
int a=45;
int *ptr;
int **ptr_ptr;

ptr=&a;
ptr_ptr=&ptr;

cout<<"\nAddress of a = "<<&a; cout<<"\nAddress of a = "<<<"\nAddress of a = "<<*ptr_ptr; cout<<"\nAddress of ptr = "<<&ptr; cout<<"\nAddress of ptr = "<<<"\nAddress of ptr_ptr = "<<&ptr_ptr; cout<<"\nValue of a = "<<<"\nValue of a = "<<*(&a); cout<<"\nValue of a = "<<*ptr; cout<<"\nValue of a = "<<**ptr_ptr; cout<<"\nValue of ptr = "<<<"\nValue of ptr_ptr = "<

Output: