edited by
522 views

1 Answer

Best answer
2 votes
2 votes

Assuming char = 1 byte and int = 4 bytes.

This question requires you to know the concept of little endian and pointer arithmetic.

Data which requires multiple bytes are stored in most modern computers in memory using the little endian system.

In little endian machines, last byte of binary representation of the multibyte data-type is stored first.

So let’s assume that the integer a with the value 300 is stored at the memory location 1000.

300 = 0b100101100

That memory location will look like this:

1000 1001 1002 1003
0010 1100 0000 0001 0000 0000 0000 0000


Now you are storing the address of an integer to a character pointer, ptr.

So ptr = 1000.

ptr++ means the pointer ptr will start pointing to the next character and not the next int (since it is a character pointer).

So, ptr = 1001.

You can use the formula ptr + 1 = ptr + 1*sizeof(*ptr) = 1000 + sizeof(char) = 1000 + 1 = 1001.

In the next line when you are de-referencing the pointer ptr, you are actually de-referencing the value stored at the memory address 1001,

So after this assignment the memory location will look like this:

1000 1001 1002 1003
0010 1100 0000 0010 0000 0000 0000 0000

 

Now when you print the value(a) that is stored at this memory address, the program will first reverse the byte order and then evaluate the binary representation as decimal.

(00000000 00000000 00000010 00101100)b in decimal = 556.

Therefore, this program will print 556.

edited by

Related questions

116
views
2 answers
1 votes
kirmada asked Jun 20
116 views
#include <stdio.h> int main() { // Write C code here int i=10,*p,**q,***r; p=&i; *p=15; q=&p; **q=20; r=&q; ***r=*p+1; printf("%d",i); return 0; }answer the output as integer _________
622
views
2 answers
0 votes
prerona_99 asked Oct 31, 2021
622 views
main () {static char *s[] = { ice , green , cone , please ,};static char **ptr[] = {s+3,s+2,s+1,s};char ***p = ptr;printf( \n%s , **++p);printf( ... \n%s , p[-1][-1]+1);} cone, ase, reenice, green, conegreen, cone, pleaseNone of the above
881
views
4 answers
4 votes
Manoj Kumar Pandey asked May 22, 2019
881 views
https://gateoverflow.in/?qa=blob&qa_blobid=14433986388826671915int main() { int a = 10; int *b = &a; scanf("%d",b); printf("%d",a+50); }What will be the Output of the following code if input given is $25$ ?
881
views
1 answers
1 votes
srestha asked May 5, 2019
881 views
$1)$ How to access array element with array of pointers? By pointer to an array we can access like this $(*a)[0]$,$(*a)[22]$, .. like thisright?but how with array of ... *(ptr+3)){ printf("Equal"); } else{ printf("Not Equal"); } return 0; }