Wednesday 27 March 2013

What is difference between uninitialized pointer and null pointer?


What is difference between uninitialized pointer and null pointer?  
Explanation:
An uninitialized pointer is a pointer which points unknown memory location while null pointer is pointer which points a null value or base address of segment. For example: 

int *p;   //Uninitialized pointer
int *q= (int *)0;  //Null pointer
#include<stdio.h>
int *r=NULL;   //Null pointer

What will be output of following c program?

#include<string.h>
#include<stdio.h>
int main(){
    char *p;  //Uninitialized pointer
    char *q=NULL;   //Null pointer;
    strcpy(p,"cquestion");
    strcpy(q,"cquestion");
    
    printf("%s  %s",p,q);
    return 0;
}

Output: cquestion (null)

Blog Author: Vijay Kumar


What is size of void pointer?


What is size of void pointer?  
Explanation:
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.

Blog Author: Vijay Kumar


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


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


Blog Author: Vijay Kumar


What is meaning of 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,"hello");
printf("%s",str);
return 0;
}

Output: (null)


Blog Author: Vijay Kumar




What is NULL pointer?


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;

Blog Author: Vijay Kumar


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


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

Explanation:
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;
}


Example 2:

#include <stdio.h>
#include <conio.h>

struct  ABC{
    int a;
    char c;
};
void main(){
clrscr();
struct ABC*temp;
printf("%d",(char*)(temp+1)-(char*)temp);
getch();
}

Blog Author: Vijay Kumar

What is pointer to a function?


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



Blog Author: Vijay Kumar

Write a c program to modify the constant variable in c?


Write a c program to modify the constant variable in c?

Explanation:
You can modify constant variable with the help of pointers. For example:

#include<stdio.h>
int main(){
   const int i=10;
    int *ptr=&i;
    *ptr=(int *)20;
    printf("%d",i);
    return 0;
}

Output: 20 


Blog Author: Vijay Kumar

What is the meaning of prototype of a function?


What is the meaning of prototype of a function?   

Explanation:
Prototype of a function

Declaration of function is known as prototype of a function. Prototype of a function means

(1) What is return type of function?
(2) What parameters are we passing?
(3) For example prototype of printf function is:

int printf(const char *, …);

I.e. its return type is int data type, its first parameter constant character pointer and second parameter is ellipsis i.e. variable number of arguments.


Blog Author: Vijay Kumar


Why we use do-while loop in c?


Why we use do-while loop in c? Also tell any properties which you know?  

Explanation:
It is also called as post tested loop. It is used when it is necessary to execute the loop at least one time. Syntax:

do {
Loop body
} while (Expression);

Example:

int main(){
    int num,i=0;
   
    do{
         printf("To enter press 1\n");
         printf("To exit press  2");
         scanf("%d",&num);
         ++i;
         switch(num){
             case 1:printf("You are welcome\n");break;
             default : exit(0);
         }
    }
    while(i<=10);
    return 0;
}

Output: 3 3 4 4

If there is only one statement in the loop body then braces is optional. For example:

(a)
int main(){
    double i=5.5678;
    do
         printf("hi");
    while(!i);
    return 0;
}

Output: 3 3 4 4

(b)
int main(){
    double i=5.63333;
    do
         printf("hi");
    while(!i);
    return 0;
}

Output: hi

(c)
int main(){
     int x=25,y=1;
     do
       if(x>5)
         printf(" ONE");
       else if(x>10)
         printf(" TWO");
       else if(x==25)
         printf(" THREE");
       else
         printf(" FOUR");
       while(y--);
return 0;
}

Output: ONE ONE




Blog Author: Vijay Kumar