Thursday, 18 April 2013

& and * program


What will be output of following program?
#include<stdio.h>
int main(){
int i = 5;
int *p;
p = &i;
printf(" %u %u", *&p , &*p);
return 0;
}
(A) 5 Address

(B) Address Address

(C) Address 5

(D) Compilation error

(E) None of above


________________________________________
Explanation:

Turbo C++ 3.0: Address Address

Turbo C ++4.5: Address Address

Linux GCC: Address Address

Visual C++: Address Address



Since * and & always cancel to each other.

i.e. *&a = a

so *&p = p which store address of integer i

&*p = &*(&i) //since p = &i

= &(*&i)

= &i

So second output is also address of i

Sunday, 14 April 2013

Pointer Variables Program


What will be output of following program?

#include<stdio.h>
#include<string.h>
int main(){
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf("%d" , c);
return 0;
}
(A) 1

(B) 5

(C) -5

(D) Compilation error

(E) None of above


________________________________________
Explanation:

Turbo C++ 3.0: 1

Turbo C ++4.5: 1

Linux GCC: 1

Visual C++: 2



Difference of two same type of pointer is always one.

Register Variable Program


What will be output of following program?

#include<stdio.h>
#include<string.h>
int main(){
int register a;
scanf("%d",&a);
printf("%d",a);
return 0;
}
//if a=25

(A) 25

(B) Address

(C) 0

(D) Compilation error

(E) None of above


________________________________________
Explanation:
Turbo C++ 3.0: Compilation error

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error

Visual C++: Compilation error



Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.

Void Pointer Program



#include<stdio.h>
int main(){
int a = 10;
void *p = &a;
int *ptr = p;
printf("%u",*ptr);
return 0;
}
(A) 10

(B) Address

(C) 2

(D) Compilation error

(E) None of above


________________________________________
Explanation:

Turbo C++ 3.0: 10

Turbo C ++4.5: 10

Linux GCC: 10

Visual C++: 10



Void pointer can hold address of any data type without type casting. Any pointer can hold void pointer without type casting.

Register variable program


What will be output of following program?

#include<stdio.h>
#include<string.h>
int main(){
register a = 25;
int far *p;
p=&a;
printf("%d ",*p);
return 0;
}
(A) 25

(B) 4

(C) Address

(D) Compilation error

(E) None of above


________________________________________
Explanation:

Turbo C++ 3.0: Compilation error

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error

Visual C++: Compilation error



Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.