pointers in c++
TRANSCRIPT
POINTERS IN C++BY - SAI TARLEKAR
Points Introduction. Declaration and initialization of pointers. Small program. Thumb rule. Pointer Variable. Pointer Arithmetic. Reference Variable. Reference as function arguments
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.
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.
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.
Program
Output:-
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.
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.
Pointer variableConsists of two parts
Pointer operator
(*) asteriskAddress operator
(&) ampersand
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.
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;
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.
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
Program
Output:-
REFERENCE VARIABLEA 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.
10sum
num
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:101000
REFERENCE AS FUNCTION ARGUMENTS
y
100
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<<"\n\nBefore Swapping : ";cout<<"\n\t\t\ta = "<<a;cout<<"\n\t\t\tb = "<<b;swap(&a,&b);
cout<<"\n\nAfter Swapping : ";cout<<"\n\t\t\ta = "<<a;cout<<"\n\t\t\tb = "<<b;getch();}swap(int *x,int *y){int temp;temp=*x;*x=*y;*y=temp;}
Output:-
Thank you