GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
93 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 in Programming by Veteran (13k points)   | 93 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 by Veteran (47k 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 May 2017
  1. akash.dinkar12

    3166 Points

  2. pawan kumarln

    1648 Points

  3. sh!va

    1600 Points

  4. Arjun

    1380 Points

  5. Bikram

    1372 Points

  6. Devshree Dubey

    1272 Points

  7. Debashish Deka

    1132 Points

  8. Angkit

    1044 Points

  9. LeenSharma

    900 Points

  10. srestha

    714 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 May 22 - 28
  1. Bikram

    458 Points

  2. pawan kumarln

    274 Points

  3. Ahwan

    236 Points

  4. Arnab Bhadra

    234 Points

  5. bharti

    190 Points


22,778 questions
29,106 answers
65,165 comments
27,647 users