Professional Documents
Culture Documents
Presented By:-
Sumit Kumar Nager
PGT Computer Science
… … 100 … … …
a Variable a’s value, i.e., 100, is
int a = 100; stored at memory location 1024
Int a,*p;
a= 100;
p=&a;
… … 100 … 1024 …
integer
pointer
• type_of_stored_data * pointer_variable_name;
• For example,
• char* chName;
• int * nTypeOfCar;
• float *fValue;
• type_of_stored_data is any valid pointer base type such as char, int, float or
other valid C derived types such as array and struct.
• It indicates the type of the variable’s data to which the pointer is pointed to.
• The pointer_variable_name follows the same rules as other C variable
naming convention and must be unique.
PGT Computer Science Created By Sumit Kumar Nager 8
Address Operator &
The "address of " operator (&) gives the memory address of the variable
Usage: &variable_name
… … 100 … … …
a
… 88 100 … … …
#include <iostream> a b
using namespace std;
void main()
Result is:
{ int a, b; The address of a is:1020
a = 88; The address of b is:1024
b = 100;
cout << "The address of a is: " << &a << endl;
cout << "The address of b is: " << &b << endl;
}
PGT Computer Science Created By Sumit Kumar Nager 10
Pointer Variables
Memory address:
1020 1024 1032
… 88 100 … 1024 …
a p
int a = 100;
Result is:
int *p = &a;
100 1024
cout << a << " " << &a <<endl; 1024 1032
cout << p << " " << &p <<endl;
… 88 100 … 1024 …
a p
int a = 100;
int *p = &a; Result is:
cout << a << endl; 100
cout << &a << endl; 1024
1024 100
cout << p << " " << *p << endl; 1032
cout << &p << endl;
PGT Computer Science Created By Sumit Kumar Nager 12
Pointer to Pointer
58 58 58
a gets 18 p
cout << "value1==" << value1 << "/ value2==" << value2<<endl;
cout<<*p1<<“ ”<<*p2;
return 0;
}
PGT Computer Science Created By Sumit Kumar Nager 15
Reference Variables
A reference is an additional name to an existing
memory location
Pointer: Reference:
x 9 x 9
ref
ref
int x = 9;
int x=9;
int &ref = x;
int *ref;
ref = &x;
PGT Computer Science Created By Sumit Kumar Nager 16
Reference Variables
• A reference variable always refers to the same
object. Assigning a reference variable with a new
value actually changes the value of the referred
object.
• Reference variables are commonly used for
parameter passing to a function
1000
1004
1008
1012
1016
Output:
Address of a[0]: 0x22feec
Name as pointer: 0x22feec
PGT Computer Science Created By Sumit Kumar Nager 21
Dereferencing An Array Name
This element is called a[0] or *a
#include <iostream>
a[0]
22 using namespace std;
a[0] 2 44
int main()
a[1] 4 p { int a[5] = {2,4,6,8,22};
a[2] 6 int *p = &a[1];
a[3] 8
p[0]
cout << a[0] << " " << p[-1];
a[4] 22 cout << a[1] << " " << p[0];
return 0;
}
a[0] or *(a + 0) 2 a
a[1] or *(a + 1) 4 a+1
a[2] or *(a + 2) 6 a+2
a[3] or *(a + 3) 8 a+3
a[4] or *(a + 4) 22 a+4
Operation Description
You can add an integer to a pointer to
4. Incrementing
point to a different memory location.
You can subtract an integer from a
5. Differencing pointer to point to a different memory
location.
Valid only with two pointers that point
6. Comparison
to the same array.
PGT Computer Science Created By Sumit Kumar Nager 30
Fun Program
#include<stdio.h> #include<iostream>
int main() using namespace std;
{ int i=327; int main()
char *p=&i; { int i=327;
*++p=2; char *p=&i;
printf("%d", i); *++p=2;
return 0; cout<<i;
} return 0;
}
new
delete
int* ptr;
{
ptr = new int[200];
int a[200];
…
…
delete [] ptr;
}
new
int *p, n=10;
p
p = new int;
new
p = new int[100];
p
new
p = new int[n]; p
PGT Computer Science Created By Sumit Kumar Nager 39
Freeing (or deleting) Memory
delete [] A;
B[0] = 1; // illegal! Locations do not belong to the program
A
?
B
-- -- -- -- --
PGT Computer Science Created By Sumit Kumar Nager 42
Memory De-allocation