This document discusses pointers in C++. It defines pointers as variables that store memory addresses of other variables. It covers declaring and initializing pointers, using the address and dereference operators, pointer arithmetic, references, and passing pointers as function arguments. The document includes examples of pointer code and output to demonstrate these concepts.
2. Points
Introduction.
Declaration and initialization of pointers.
Small program.
Thumb rule.
Pointer Variable.
Pointer Arithmetic.
Reference Variable.
Reference as function arguments
3. What is a Pointer???
A pointer is a variable which holds the memory address
of another variable.
This memory address is the location of another variable
where it has been stored as memory.
Therefore if a variable contains the address of another
variable is said to point to the second.
4. Declaration and Initialization of
Pointers
Syntax : Datatype *variable_name;
eg. int *x;
float *y;
char *z;
eg. int x = 10;
int *ptr = &x;
Now ptr will contain address where the variable x is stored in memory.
5. int *ptr;
This tells the compiler three things about ptr:-
1) The (*) tells that the variable ptr is a pointer
variable.
2) Pointers need a memory location.
3) Pointers points to a variable of type int.
9. More info….
1) A pointer that is not initialzed is called as wild pointer
because you have no idea what it is pointing to.
2) A pointer whose value is zero is called as null pointer.
3) Void pointer - void pointer is a special type of pointer
that can be pointed at objects of any data type. A void
pointer is declared using data type void.
10. Thumb rule of pointers:-
1) Type must be same to the variable whose address is going
to pass to the pointer.
2) When you want to pass the address of the variable use ‘&’
before the variable name.
3) When you want to pass the value of the variable use ‘*’
before the pointer.
11.
12. Pointer operator
A pointer operator or a pointer variable is represented by a
combination of asterisk(*) with a variable name.
Syntax:-
data_type *pointer_variable;
eg. If a variable of character data type and also declared as *
(asterisk) with another variable it means the variable is of type
“Pointer to character”.
char *ptr;
where ptr is a pointer variable which holds the address of an
character data type.
13. Address Operator
An address operator can be represented by a combination of and (ampersand)
with a pointer variable.
The and is a unary operator that returns the memory address of its operand.
Syntax:-
data_type variable1;
data_type variable=&variable1;
Eg:-
int x;
int *ptr=&x;
It means that ptr receives the address of x;
14. Pointer Arithmetic
Two arithmetic operations, addition and subtraction, may be
performed on pointers. When we add 1 to a pointer, we are
actually adding the size of data type in bytes, the pointer is
pointing at.
Eg. int *x; x++;
If current address of x is 1000, then x++ statement will increase x
by 2(size of int data type) and makes it 1002, not 1001.
15. Data is stored in contiguous memory cell.
The Number of memory cells required to store a
data item depends on the type of data item
Char 1 byte
Integer 2 byte
Floating 4 byte
Double 8 byte
18. REFERENCE VARIABLE
A reference variable is a name that acts as an alias or an alternative name,
for an already existing variable.
Syntax:-
Data type &variable name = already existing variable;
Eg. int num=10;
int & sum = num; // sum is a reference variable or alias name for
num
NOTE: Both num and sum refer to the same memory location. Any changes made to the sum
will also be reflected in num.
10
sum
num
19. void cube(int &x)
{
x= x*x*x;
}
void main()
{
int y=10;
cout<<y<<endl;
cube(y);
cout<<y<<endl;
}
x
In the above program reference of y is passed. No separate
memory is allocated to x and it will share the same memory
as y. Thus changes made to x will also be reflected in y.
OUTPUT:
10
1000
REFERENCE AS FUNCTION ARGUMENTS
y
100
20. Program
#include<iostream.h>
#include<conio.h>
swap(int *,int *)
void main()
{
int a,b;
cout<<"nEnter value for A : ";
cin>>a;
cout<<"nEnter value for B : ";
cin>>b;
cout<<"nnBefore Swapping : ";
cout<<"nttta = "<<a;
cout<<"ntttb = "<<b;
swap(&a,&b);
cout<<"nnAfter Swapping : ";
cout<<"nttta = "<<a;
cout<<"ntttb = "<<b;
getch();
}
swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}