Pointers
Pointers
Pointers
syntax :
data_type *<identifier>;
ex :
int *ptr (or) int* ptr;
In the above declaration "ptr" is a pointer variable which can stores address of integer data.
2) Untyped pointer : An untyped pointer can points to any type of data. It is also called Generic pointer.
void* ----> any data
"*":
It is called pointer operator.
It returns the data which has stored inside the specific address.
#include<stdio.h>
void main()
{
int i=100;
int* ptr ;
ptr = &i;
printf("%d\n",i);
printf("%u\n",ptr);
printf("%u\n",&i);
printf("%u\n",&ptr);
printf("%d\n",*ptr);
printf("%d\n",*(&i));
printf("%d\n",*(*(&ptr)));
}
Size of pointer :
Size of pointer is equals to size of integer on the machine.
Pointer size also varies from compiler to compiler like integer.
#include<stdio.h>
struct st
{
int i ;
char c ;
float f ;
};
void main()
{
short* sp ;
char* cp ;
struct st* stp;
Call by value :
Calling a function by passing values as parameters.
These values will be collected in to formal arguments.
Formal arguments are working like local variables of function.
Data processed inside the Called function will modify formal arguments.
Draw back of "Call by value" is, the processed data in the "Called function" cannot be accessed
in the "Calling function".
#include<stdio.h>
void swap(int,int);
void main()
{
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("Before swap : \t%d\t%d\n",a,b);
swap(a,b);
}
Call by reference :
Calling a function by passing address(reference) as parameter.
We collect these paremeters into arguments of Pointer type.
Using these pointers we can process the information of calling function directly.
We can access the processed information either from calling function or called function.
#include<stdio.h>
void swap(int*,int*);
void main()
{
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("Before swap : \t%d\t%d\n",a,b);
swap(&a,&b);
printf("after swap : \t %d \t %d \n",a,b);
}
void swap(int* x, int* y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
printf("after swap : \t %d \t %d \n",*x,*y);
}
Pointer to function :
It is also possible to create pointers to Derived data types such as function, array, string .....
To execute block of instructions, OS allocates the memory in stack area is called "FRAME".
Every "FRAME" has a base address, that is working like entry point to execute function.
Pointer to function variable holds a reference of FRAME.
Syntax :
<return_type> (*<ptr_name>)(args_list);
Example :
int (*fptr)(int,int);
In the above declaration "fptr" is a pointer can points to any function which is taking two integer
arguments and returns integer-data.
#include<stdio.h>
int add(int,int);
int sub(int,int);
void main()
{
int r1,r2,r3,r4;
int (*fptr)(int,int);
r1=add(10,20);
r2=sub(10,20);
printf("r1 : %d\nr2 : %d\n",r1,r2);
The above declaration describes "fptr" is a function which is taking two integer arguments and
returning "address of integer data".
#include<stdio.h>
int* add(int,int);
void main()
{
int a,b;
int* c;
printf("enter two numbers : ");
scanf("%d%d",&a,&b);
c = add(a,b);
printf("sum : %d\n",*c);
}
int* add(int x, int y)
{
int z;
z=x+y;
return &z;
}
Pointer type-casting :
Typed pointers can points to specific type of data.
We cannot point one type of data using another type of pointer.
We need to type cast pointers to make it points to another kind of data.
Generic pointer(void*) can implicitly type casted to any other pointer type. Hence type
conversion is not required for void*.
#include<stdio.h>
void main()
{
int i = 100;
int* ip;
char* cp;
ip = &i;
cp = (char*)ip;
printf("i value : %d\n", *ip);
printf("i value : %d\n", *cp);
}
Pointer arithmetic :
Modifying operators used to increase or decrease the value of a variable.
Increment operators increases the value of a variable by 1
Decrement operators decreases the value of a variable by 1
When we modify a pointer variable using modifying operators, the value of variable will be
increased or decreased by "size" bytes depends on data stored inside the location.
#include<stdio.h>
void main()
{
short sa[5]={10, 20, 30, 40, 50};
char ca[5]={'a', 'b', 'c', 'd', 'e'};
short* sp;
char* cp ;
sp = sa ;
cp = ca ;
Pointer to array :
When a pointer pointing to the array we use many ways to access elements.
#include<stdio.h>
void main()
{
int iarr[5] = {10,20,30,40,50},i;
int* ptr;
ptr = iarr;
printf("elements are :\n");
for(i=0 ; i<5 ; i++)
{
printf("%d,%d,%d,%d,%d\n", iarr[i], ptr[i], *(ptr+i), *(i+ptr), i[ptr]);
}
}
Note : Priority of operators need to be considered while evaluating the expressions to access the
elements of array using pointers.
First priority : ++ , --
Second priority : *
Third priority : arithmetic operators
Note : ( ) having higher priority than all.
#include<stdio.h>
void main()
{
int arr[5] = {10,20,30,40,50},i;
int* ptr;
ptr = arr;
printf("%u\n", *++ptr + 3);
printf("%u\n", *(ptr-- + 2) + 5);
printf("%u\n", *(ptr+3)-10);
}
#include<stdio.h>
void main()
{
int arr[5] = {8, 3, 4, 9, 2},i;
int* ptr;
ptr = arr;
printf("%u\n", *(--ptr+2) + 3);
printf("%u\n", *(++ptr + 2) - 4);
printf("%u\n", *(ptr-- +1 ) + 2);
}
Array of pointers :
Array of pointers variable holds more than one element address.
#include<stdio.h>
void main()
{
int iarr[5] = {10,20,30,40,50},i;
int* ptr[5];
for(i=0 ; i<5; i++)
{
ptr[i] = &iarr[i];
//ptr[i] = iarr+i;
}
printf("array elements are \n");
for(i=0 ; i<5; i++)
{
printf("%d\n", *ptr[i]);
//printf("%d\n", *(*(ptr+i)));
}
}
pointer to structure :
It is allowed to create pointers to user-defined data types also such as structures and unions.
when a pointer pointing to structure,we use "->" operator to access the elements instead of "."
operator.
#include<stdio.h>
struct emp
{
int eno;
char ename[20];
float esal;
};
void main()
{
struct emp e;
struct emp* ptr;
ptr = &e;
pointer to string :
A char* can points to
1) a single character data
2) or a block of characters(String)
#include<stdio.h>
void main()
{
char* name = "nareshit";
printf("%s\n",name);
printf("%c\n",name);
printf("%c\n",*name);
}
#include<stdio.h>
void main()
{
char* str = "learnown";
printf("%c\n", *str++ + 3);
printf("%s\n", ++str+2);
}
#include<stdio.h>
void main()
{
char* str = "learnown";
printf("%c\n" , *(str++ + 2)+3);
printf("%c\n" , *++str+2);
printf("%s\n" , --str-1);
}
#include<stdio.h>
void main()
{
char sport[]= "cricket";
int x=1 , y;
y=x++ + ++x;
printf(“%c”,sport[++y]);
}
Two dimensional array of characters: We can easily process the 2 dimension array of characters often
called array of strings using pointers.
#include<stdio.h>
void main()
{
char* names[ ] = {"one", "two", "three", "four"};
int i;
for(i=0 ; i<4 ; i++)
{
printf("%s\n",names[i]);
printf("%c\n",*names[i]);
}
}
pointer to pointer :
A pointer to pointer type varible holds address of another pointer variable.
#include<stdio.h>
void main()
{
int i = 100;
int* ip;
int** ipp;
ip = &i;
ipp = &ip;
printf("%d,%d,%d",i,*ip,**ipp);
}
#include<stdio.h>
void main()
{
char *s[ ] = {"black", "white", "pink", "violet"};
char **ptr[ ] = {s+3, s+2, s+1, s};
char ***p;
p = ptr;
++p;
printf("%s\n", (*(*p+1)+1)+2);
}
#include<stdio.h>
void main()
{
char *s[ ]={"black", "white", "pink", "violet"};
char **ptr[ ] = {s+1, s, s+3, s+2};
char ***p;
p = ptr;
p+1;
printf("%c\n", *(*(*++p+1))+3);
}
1 (),[] left->right
2 *, identifier right->left
3 data_type ....
ptr is a pointer pointing to a function, which is taking two integer arguments and returns integer
data.
3) int ( * ( * ptr ) [ 5 ] ) ( )
6 4 21 3 5
"ptr" is a pointer pointing to an array of size 5 which is holding pointers to functions which are
taking no arguments and returns integer data.