Pointer to function in c programming
What will be output if you will execute following code?
int * function();
void main(){
auto int *x;
int *(*ptr)();
ptr=&function;
x=(*ptr)();
printf("%d",*x);
}
int *function(){
static int a=10;
return &a;
}
Output: 10
Explanation: Here
function is function whose parameter is void data type and return type is
pointer to int data type.
x=(*ptr)()
=>
x=(*&functyion)() //ptr=&function
=>
x=function() //From rule *&p=p
=>
x=&a
So, *x =
*&a = a =10
What will be output if you will execute following code?
int find(char);
int(*function())(char);
void main(){
int x;
int(*ptr)(char);
ptr=function();
x=(*ptr)('A');
printf("%d",x);
}
int find(char c){
return c;
}
int(*function())(char){
return find;
}
Output: 65
Explanation: Here
function whose name is function which passing void data type and returning
another function whose parameter is char data type and return type is int data
type.
x=(*ptr)(‘A’)
=>
x= (*function ()) (‘A’) //ptr=function ()
//&find=function () i.e. return
type of function ()
=>
x= (* &find) (‘A’)
=>
x= find (‘A’) //From rule*&p=p
=>
x= 65
What will be output if you will execute following code?
char * call(int *,float *);
void main(){
char *string;
int a=2;
float b=2.0l;
char *(*ptr)(int*,float *);
ptr=&call;
string=(*ptr)(&a,&b);
printf("%s",string);
}
char *call(int *i,float *j){
static char *str="choudharyrajveer.blogspot.com";
str=str+*i+(int)(*j);
return str;
}
Output: dharyrajveer.blogspot.com
Explanation: Here
call is function whose return type is pointer to character and one parameter is
pointer to int data type and second parameter is pointer to float data type and
ptr is pointer to such function.
str=
str+*i+ (int) (*j)
=”choudharyrajveer.blogspot.com”
+ *&a+ (int) (*&b)
//i=&a,
j=&b
=”choudharyrajveer.blogspot.com”
+ a+ (int) (b)
=”choudharyrajveer.blogspot.com”
+2 + (int) (2.0)
=”choudharyrajveer.blogspot.com”
+4
=”dharyrajveer.blogspot.com”
What will be output if you will execute following code?
char far * display(char far*);
void main(){
char far*
string="cquestionbank.blogspot.com";
char far
*(*ptr)(char far *);
ptr=&display;
string=(*ptr)(string);
printf("%s",string);
}
char far *display(char far * str){
char far
* temp=str;
temp=temp+13;
*temp='\0';
return str;
}
Output: cquestionbak
Explanation: Here
display is function whose parameter is pointer to character and return type is
also pointer to character and ptr is its pointer.
temp
is char pointer
temp=temp+13
temp=’\0’
Above
two lines replaces first dot character by null character of string of variable
string i.e.
"cquestionbank\0blogspot.com"
As
we know %s print the character of stream up to null character.
Pointer to Array of Function in C
Array of function means array which content is address of function.
What will be output if you will execute following code?
#include"conio.h"
int display();
int(*array[3])();
int(*(*ptr)[3])();
void main(){
array[0]=display;
array[1]=getch;
ptr=&array;
printf("%d",(**ptr)());
(*(*ptr+1))();
}
int display(){
int x=5;
return x++;
}
Output: 5
Explanation:
In
this example:
array
[]: It is array of pointer to such function which parameter is void and return
type is int data type.
ptr:
It is pointer to array which contents are pointer to such function which
parameter is void and return type is int type data.
(**ptr)() = (** (&array)) () //ptr=&array
=
(*array) () // from rule *&p=p
=array
[0] () //from rule *(p+i)=p[i]
=display
() //array[0]=display
(*(*ptr+1))() =(*(*&array+1))() //ptr=&array
=*(array+1)
() // from rule *&p=p
=array
[1] () //from rule *(p+i)=p[i]
=getch
() //array[1]=getch
Pointer to array of string in c
programming
What will be output if you will execute following code?
void main(){
char *array[4]={"c","c++","java","sql"};
char *(*ptr)[4]=&array;
printf("%s ",++(*ptr)[2]);
}
Output: ava
Explanation:
In
this example
ptr:
It is pointer to array of string of size 4.
array[4]:
It is an array and its content are string.
++(*ptr)[2]
=++(*&array)[2] //ptr=&array
=++array[2]
=++”java”
=”ava”
//Since ptr is character pointer so it
//
will increment only one byte
Note: %s is used to print stream of characters up to null
(\0) character.
Pointer to Structure in C Programming
What will be output if you will execute following code?
struct address{
char *name;
char street[10];
int pin;
}cus={"A.Kumar","H-2",456003},*p=&cus;
void main(){
printf("%s
%s",p->name,(*p).street);
}
Output: A.Kumar
H-2
Explanation:
p
is pointer to structure address.
->
and (*). Both are same thing. These operators are used to access data member of
structure by using structure’s pointer.
Pointer to Union in C Programming
What will be output if you will execute following code?
union address{
char *name;
char street[10];
int pin;
};
void main()
{
union address
emp,*p;
emp.name="ja\0pan";
p=&emp;
printf("%s %s",p->name,(*p).name);
}
Output: ja
ja
Explanation:
p
is pointer to union address.
->
and (*). Both are same thing. These operators are used to access data member of
union by using union’s pointer.
%s
is used to print the string up to null character i.e. ‘\0’
Multilevel Pointers in C Programming
What will be output if you will execute following code?
void main()
{
int s=2,*r=&s,**q=&r,***p=&q;
printf("%d",p[0][0][0]);
}
Output: 2
Explanation:
As
we know p[i] =*(p+i)
So,
P[0][0][0]=*(p[0][0]+0)=**p[0]=***p
Another
rule is: *&i=i
So,
***p=***
(&q) =**q=** (&r) =*r=*(&s) =s=2
What will be output if you will execute following code?
#define int int*
void main(){
int *p,q;
p=(int *)5;
q=10;
printf("%d",q+p);
}
Output: 25
Explanation: If
you will see intermediate file you will find following code:
void main(){
int **p,q;
p=(int **)5;
q=10;
printf("%d",q+p);
}
Here
q pointer and p is a number.
In
c
Address
+ number = Address
So,
New
address = old address + number * Size of data type to which pointer is
pointing.
= 5 + 10 * sizeof (*int)
= 5+10*2 = 25.
Note. We are assuming default pointer is near. Actually it
depends upon memory model.
Pointer to Array of Pointer to String in C Programming
What will be output if you will execute following code?
void main(){
static char *s[3]={"math","phy","che"};
typedef char *(
*ppp)[3];
static ppp
p1=&s,p2=&s,p3=&s;
char *
(*(*array[3]))[3]={&p1,&p2,&p3};
char *
(*(*(*ptr)[3]))[3]=&array;
p2+=1;
p3+=2;
printf("%s",(***ptr[0])[2]);
}
Output: che
Explanation:
Here
ptr:
is pointer to array of pointer to string.
P1,
p2, p3: are pointers to array of string.
array[3]:
is array which contain pointer to array of string.
As
we know p[i]=*(p+i)
(***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2]
=(***(&array))[2] //ptr=&array
=(**array)[2] //From
rule *&p=p
=(**(&p1))[2] //array=&p1
=(*p1)[2]
=(*&s)[2] //p1=&s
=s[2]=”che”
Pointer to Three Dimensional Array
in C Programming
void main(){
const array[2][3][3]={0,1,2,3,4,5,6,7,8,9,10,11,12};
int const (*ptr)[2][3][3]=&array;
printf("%d ",*(*(*ptr)[1]+2));
}
Output: 11
Explanation:
In
this example:
array
[2][3][3]:It is three dimensional array and its content are constant integers.
ptr:
It is pointer to such three dimensional array whose content are constant
integer.
*(*(*ptr)
[1] +2)
=*(*(*&array)
[1] +2)
=*(*array
[1] +2)
=*(array
[1] [0] +2)
=array
[1] [0] [2]
I.e.
array element at the 1*(3*3) +0(3) + 2=11th position starting
from zero which is 11.
========================================================================
Pointer to Two Dimensional Array in C Programming
What will be output if you will execute following code?
void main(){
long array[][3]={7l,14l,21l,28l,35l,42l};
long int (*ptr)[2][3]=&array;
printf("%li ",-0[1[0[ptr]]]);
}
Output: -28
Explanation:
-0[1[0[ptr]]]
=-1[0[ptr]][0] //From
rule array[i]=i[array]
=-0[ptr][1][0]
=-ptr
[0] [1] [0]
=-*ptr
[0] [1] //From rule array[i]=*(array+i)
=-*(&array)
[0] [1]
=-(&array)
[0] [1][0]
=-(*&array)[1][0] //From
rule *&p=p
=-array[1][0]
array[1][0]
means 1*(3)+ 0 = 3rd element of array starting from zero i.e.
28
Sorting of Array Using Pointer in C Programming
void main()
{
int i,j,temp1,temp2;
int arr[8]={5,3,0,2,12,1,33,2};
int *ptr;
for(i=0;i<7;i++)
{
for(j=0;j<7-i;j++)
{
if(*(arr+j)>*(arr+j+1))
{
ptr=arr+j;
temp1=*ptr++;
temp2=*ptr;
*ptr--=temp1;
*ptr=temp2;
}
}
}
clrscr();
for(i=0;i<8;i++)
printf("
%d",arr[i]);
getch();
}
Output: 0 1 2 2 3 5 12 33
What will be output if you will execute following code?
void main()
{
static float farray[][3]={0.0f,1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f};
float (*array[3])[3]={&farray[0],&farray[1],&farray[2]};
float (*(*ptr)[])[3]=&array;
printf("%f
",2[(*(**ptr+1))]);
}
Output: 5.000000
Explanation:
In
this example:
farray
[][3]: It is two dimension array and its content are float constants.
array
[3]:It is one dimension array and its content are address of such one dimension
array which content are float constant.
ptr:
It is pointer to one dimension array which content are address of such one
dimension array which content are float constant.
2[(*(**ptr+1))]
=
(*(**ptr+1)) [2]
=
(*(**&array+1)) [2]
=
(*(*array+1)) [2]
=
(*(array [0] +1)) [2]
=
(*(&farray [0] +1)) [2]
=&farray
[0] [1] [2]
=*&farray
[1] [2]
=farray
[1] [2]
It
is 1*(3) +2=5th element of farray starting from zero which is
5.0f
Pointer to array of union in c
programming
What will be output if you will execute following code?
union emp{
char *name;
int id;
};
void main(){
static union emp
e1={"A"},e2={"B"},e3={"C"};
union emp(*array[])={&e1,&e2,&e3};
union emp(*(*ptr)[3])=&array;
printf("%s ",(*(*ptr+2))->name);
}
Output: C
Explanation:
In
this example:
e1,
e2, e3: They are variables of union emp.
array
[]:It is one dimensional array of size thee and its content are address of
union emp.
ptr:
It is pointer to array of union.
(*(*ptr+2))->name
=(*(*&array+2))->name //ptr=&array
=(*(array+2))->name //from
rule *&p=p
=array[2]->name //from
rule *(p+i)=p[i]
=(&e3)->name //array[2]=&e3
=*(&e3).name //from
rule ->= (*).
=e3.name //from
rule *&p=p
=”C”
Pointer to array of structure in c
programming
What will be output if you will execute following code?
struct emp{
char *name;
int id;
};
void main(){
static struct emp
e1={"A",1},e2={"B",2},e3={"C",3};
struct emp(*array[])={&e1,&e2,&e3};
struct emp(*(*ptr)[3])=&array;
printf("%s
%d",(**(*ptr+1)).name,(*(*ptr+1))->id);
}
Output: B
2
Explanation:
(**(*ptr+1)).name
=(**(*&array+1)).name //ptr=&array
=(**(array+1)).name //from
rule *&p =p
=(*array[1]).name //from
rule *(p+i)=p[i]
=(*&e2).name //array[1]=&e2
=e2.name=”B” //from
rule *&p =p
(*(*ptr+1))->id
=(**(*ptr+1)).id //from
rule -> = (*).
=e2.id=2
Pointer to Array of Character in C Programming
What will be output if you will execute following code?
char display(char (*)[]);
void main()
{
char c;
char character[]={65,66,67,68};
char (*ptr)[]=&character;
c=display(ptr);
printf("%c",c);
}
char display(char (*s)[])
{
**s+=2;
return **s;
}
Output: C
Explanation: Here
function display is passing pointer to array of characters and returning char data
type.
**s+=2
=>**s=**s+2
=>**ptr=**ptr+2 //s=ptr
=>**&character=
**&character+2 //ptr=&character
=>*character=*character+2 //from
rule *&p =p
=>character[0]=character[0]+2 //from
rule *(p+i)=p[i]
=>character
[0] =67
**s=character
[0] =67
Note: ASCII value of ‘C’ is 67
Pointer to array
What will be output if you will execute following code?
void main()
{
static int i,j,k;
int *(*ptr)[];
int *array[3]={&i,&j,&k};
ptr=&array;
j=i+++k+10;
++(**ptr);
printf("%d",***ptr);
}
Output: 10
Explanation:
In
this example:
array
[]: It is array of size three and its content are address of integer.
ptr:
It is pointer to array which content are address of integer.
j=i+++k+10
=i++
+ k+10
=0
+0 +10=10
***ptr = *** (&array) //ptr=&array
=
**array //From rule *&p=p
//From
rule array [0] =*(array+0) and ++ (**ptr)
=*array
[1]
=*&j
=j
=10
What will be output if you will execute following code?
void main()
{
int i,j,k;
int *(*ptr)[];
int *array[3]={&i,&j,&k};
ptr=&array;
j=i+++k+10;
++(**ptr);
printf("%d",***ptr);
}
Output: Compiler
error
Explanation: Address
of auto variable cannot be member of an array.