C Language ---(E)Programming Interview Questions----3

11. What is pointer to a function? 

Explanation:
(1) What will be output if you will execute following code?
int * function();
int 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
(2) What will be output if you will execute following code?
int find(char);
int(*function())(char);
int main(){
int x;
int(*ptr)(char);
ptr=function();
x=(*ptr)('A');
printf("%d",x);
return 0;
}
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
(3) What will be output if you will execute following code?
char * call(int *,float *);
int main(){
char *string;
int a=2;
float b=2.0l;
char *(*ptr)(int*,float *);
ptr=&call;
string=(*ptr)(&a,&b);
printf("%s",string);
return 0;
}
char *call(int *i,float *j){
static char *str="turboc.codeplex.com";
str=str+*i+(int)(*j);
return str;
}
Output: turboc.codeplex.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)
=”turboc.codeplex.com” + *&a+ (int) (*&b)
//i=&a, j=&b
=”turboc.codeplex.com” + a+ (int) (b)
=”turboc.codeplex.com” +2 + (int) (2.0)
=”turboc.codeplex.com” +4
=”turboc.codeplex.com”
(4) What will be output if you will execute following code?
char far * display(char far*);
int main(){
char far* string="turboc.codeplex.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: turboc.codep
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.
"turboc.codepl\0ex.com"
As we know %s print the character of stream up to null character.

12. Write a c program to find size of structure without using sizeof operator?

struct ABC{
 int a;
 float b;
 char c;
};
int main(){
 struct ABC *ptr=(struct ABC *)0;
 ptr++;
 printf("Size of structure is: %d",*ptr);
 return 0;
}
 

13. What is NULL pointer? 

Explanation:
Literal meaning of NULL pointer is a pointer which is pointing to nothing. NULL pointer points the base address of segment.
Examples of NULL pointer:
1. int *ptr=(char *)0;
2. float *ptr=(float *)0;
3. char *ptr=(char *)0;
4. double *ptr=(double *)0;
5. char *ptr=’\0’;
6. int *ptr=NULL;
What is meaning of NULL?
Answer:
NULL is macro constant which has been defined in the heard file stdio.h, alloc.h, mem.h, stddef.h and stdlib.h as
#define NULL 0
Examples:
(1)What will be output of following c program?
#include "stdio.h"
int main(){
if(!NULL)
printf("I know preprocessor");
else
printf("I don't know preprocessor");
}
Output: I know preprocessor
Explanation:
!NULL = !0 = 1
In if condition any non zero number mean true.
(2)What will be output of following c program?
#include "stdio.h"
int main(){
int i;
static int count;
for(i=NULL;i<=5;){
count++;
i+=2;
}
printf("%d",count);
}
Output: 3
(3)What will be output of following c program?
#include "stdio.h"
int main(){
#ifndef NULL
#define NULL 5
#endif
printf("%d",NULL+sizeof(NULL));
}
Output: 2
Explanation:
NULL + sizeof(NULL)
=0 + sizeoof(0)
=0+2 //size of int data type is two byte.
We cannot copy anything in the NULL pointer.
Example:
(4)What will be output of following c program?
#include "string.h"
int main(){
char *str=NULL;
strcpy(str,"c-pointer.blogspot.com");
printf("%s",str);
return 0;
}
Output: (null)

14. What is difference between pass by value and pass by reference?  

Explanation:
In c we can pass the parameters in a function in two different ways.
(a)Pass by value: In this approach we pass copy of actual variables in function as a parameter. Hence any modification on parameters inside the function will not reflect in the actual variable. For example:
#include<stdio.h>
int main(){
 int a=5,b=10;
 swap(a,b);
 printf("%d %d",a,b);
 return 0;
} 
void swap(int a,int b){
 int temp;
 temp =a;
 a=b;
 b=temp;
}
Output: 5 10
(b)Pass by reference: In this approach we pass memory address actual variables in function as a parameter. Hence any modification on parameters inside the function will reflect in the actual variable. For example:
#incude<stdio.h>
int main(){
 int a=5,b=10;
 swap(&a,&b);
 printf("%d %d",a,b);
 return 0;
} 
void swap(int *a,int *b){
 int *temp;
 *temp =*a;
 *a=*b;
 *b=*temp;
}
Output: 10 5

15. What is size of void pointer? 

Size of any type of pointer in c is independent of data type which is pointer is pointing i.e. size of all type of pointer (near) in c is two byte either it is char pointer, double pointer, function pointer or null pointer. Void pointer is not exception of this rule and size of void pointer is also two byte.

Comments

Popular posts from this blog

[RG] Horror movies

107.John Wayne GACY Jr.

30. SERIAL KILLERS AND ASTROLOGY