GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
55 views
#include

int main()

{

char *x;

x = (char *) &a;

a = 512;

x[0] = 1;

x[1] = 2;

printf("%d\n",a);

return 0;

}

What is the output of above program?

(a) Machine dependent

b) 513

(c) 258

(d) Compiler Error
asked ago in Programming by Veteran (10.1k points)   | 55 views
Obvio it will be compilation error , you are using undeclared a
............................

1 Answer

+1 vote

Assuming a is declared first.

  • In a system that uses little-endian (Like  Intel x86 processors ) the answer should be $513$
  • In a system that uses big-endian (Like IBM z/Architecture mainframes ) the answer should be a very large value.

 Answer should be (a) Machine dependent

answered ago by Veteran (34.9k points)  
debashish,how do u know that it is endian notation??
and if  'a' is a int variable then how will x[0] and x[1] work??

i mean a is an integer type variable,so what will be x[0] or x[1] refer to??

@Akriti

A prerequisite to know what is happening http://gateoverflow.in/4273/char-pointer-to-access-an-int

thanks @kapil..
Top Users Jan 2017
  1. Debashish Deka

    7050 Points

  2. Habibkhan

    4674 Points

  3. Vijay Thakur

    4224 Points

  4. saurabh rai

    4008 Points

  5. sudsho

    3960 Points

  6. Arjun

    3108 Points

  7. GateSet

    3088 Points

  8. santhoshdevulapally

    3004 Points

  9. Bikram

    2976 Points

  10. Sushant Gokhale

    2744 Points

Monthly Topper: Rs. 500 gift card

18,810 questions
23,779 answers
51,414 comments
20,128 users