Pointer in c
• Declaration of pointer variable.
• Syntax:-
Eg:- //here a is normal variable
//here p is pointer variable
//here (p) pointer variable hold address of (a) variable.
//here u get value a i.e.10 in output by both
a and p
//it gives address of a in hexadecimal form as address in
memory store in hexadecimal form
Some points regarding declaration of
pointer variable
here p is pointer variable with symbol * which denotes p is pointer
variable and int is datatype of that variable whose address is store by pointer).
b)int* p;
c)int *p;
How to initialize a pointer variable:-
a)int a=5;
int *p;
p=&a;
printf(“%d”,*p); (here (p) print that variable value (a value)whose address
is store by pointer variable as * symbol with p in printf function print or hold
the value of normal variable).
Some more way of pointer initialization are:-
//this is wrong way of pointer initialization
TYPES OF POINTER
VOID POINTER NULL POINTER DANGLING POINTER
WILD POINTER
VOID POINTER IN C LANGUAGE
• Declaration syntax for void pointer is given below:-
• Syntax:-
//here p is void pointer
//here if we want to print 10 than void pointer p
first need to be typecast into integer data type than only it print 10.
Null pointer in c language
Declaration syntax:- datatype *variable-name=NULL;
output =0(zero) bcz we initialize pointer with NULL that itself means
0 and it means now it denotes 0th memory location which has some
address.
Wild pointer in c language
• Declaration Syntax:-
• To overcome this problem wild pointer initialized with Null pointer
Dangling pointer in c language
• Declaration syntax:-
//here p act as dangling pointer which free the memory
after use
How to do Arithmetic in pointer
//here pointer p hold base address of element at 0 index.
; //here we apply addition operator with pointer .Now in pointer to do sum
or to calculate result we apply one formula i.e. (base address +2*data-type size).
So, here base address at 0 index element suppose like 200 so calculation
is(200+2*4=208). So,at 208 address 30 value is [Link],output is 30.
print 30 as result.
• One shortcut is to go with addition number like here it is 2 and treat that
number as index of array and at that particular index whatever the value is
written that will print.
How to increment and decrement in pointer
//we can’t write p+1 bcz to write this thing we need
another variable like c=p+1