retagged by
336 views
4 votes
4 votes

What will be the output of the following program?

It is given that $\textsf{sizeof(int) = 4}$ and $\textsf{sizeof(int *) = 8.}$

#include<stdio.h> 
main(){
    void** p = 1000;
    int* q = ((int*)(p + 1)) - 1;
    printf("%u", q);
}
  1. $1000$
  2. $1008$
  3. $1004$
  4. Error, since $p+1$ is not allowed as $p$ is declared as void.
retagged by

1 Answer

Best answer
10 votes
10 votes

void **p = 1000.

p + 1 = 1000 + 1 * sizeof(*p) = 1000 + 1 * sizeof(void *) = 1000 + 1 * sizeof(int *) = 1000 + 1 * 8 = 1008.

since, void * is an address of a variable of type pointer to void. Within same system, address of any type of variable is of same length/size.

(int *)(p+1) – 1 = (int *)(1008) – 1 * sizeof(int) = 1008 – 1 * 4 = 1004.

Answer :- C.

selected by
Answer:

Related questions