1

I did this:

int i=1;
sizeof(++i);
cout<<i;

…and the output is 1. Why did the integer not increment in its value?

I know it might be a stupid question, but I didn't know where else to ask/search for the answer.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
user1076731
  • 203
  • 3
  • 14
  • 1
    The short answer is "because sizeof is a compile-time operator. The result is based only on the *type* of the argument, and the argument itself is *not* evaluated." – Jerry Coffin Dec 02 '11 at 05:07

1 Answers1

4

sizeof is determined by the compiler at compile time, and only the type of the argument matters. That's why you can have, e.g.

int *list = malloc(10*sizeof(*list));

Even though list is uninitialized at the sizeof.

Kevin
  • 53,822
  • 15
  • 101
  • 132