0

What will be the output of the following program if the address of variable 'a' is 66553?

#include<stdio.h>
include<conio.h>
int main() {
  int a=10;
  void *k;
  k=&a;
  k++;
  printf("\n%u", k);
  getch();
  return 0;
}

Why output of the above code is not 66554?

Why it will generate an error?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    void* isn't guaranteed to be of size 1. some compilers allow it to be 1, others crash. – Jean-François Fabre Sep 02 '17 at 20:09
  • 3
    You have two problems" (1) you're printing the pointer value using `%u`, which isn't guaranteed to work (especially in 64-bit or other "mixed" models). Use `%p` instead. (2) You're trying to do arithmetic on a void pointer. You really want to use `char *`, or in this case maybe `int *`. (And if you compare the difference between incrementing an `char *` versus an `int *`, you should learn something.) – Steve Summit Sep 02 '17 at 20:12
  • if you need it to be incremented by 1, use `uint8_t *` type for your pointer. – Jean-François Fabre Sep 02 '17 at 20:12
  • Only GCC allows arithmetic on `void *` (and probably Clang for GCC compatibility). The C standard does not allow arithmetic on `void *`. – Jonathan Leffler Sep 02 '17 at 20:51

0 Answers0