L
linq936
Hi,
I am puzzled on this C puzzle:
How to interpret this C statement: sizeof (int) * p
Is that the size of integer type multiplies p or the size of
whatever p points to in integer type?
I think it should be the latter because there are 4 parser tokens
here, sizeof, int, * and p, the first and the second are of same
precedence which are higher than the third and all three operators are
right associativity.
So I would parse the code into sizeof ((int) (* p))
But it does turn right. I have the following test code:
void main() {
int pp = 1;
int* p = &pp;
sizeof (int) * p;
}
I use GCC to compile it and get the following error:
main.c:4: invalid operands to binary *
So compiler takes "*" as multiplication.
Do you see at what point I am wrong?
I am puzzled on this C puzzle:
How to interpret this C statement: sizeof (int) * p
Is that the size of integer type multiplies p or the size of
whatever p points to in integer type?
I think it should be the latter because there are 4 parser tokens
here, sizeof, int, * and p, the first and the second are of same
precedence which are higher than the third and all three operators are
right associativity.
So I would parse the code into sizeof ((int) (* p))
But it does turn right. I have the following test code:
void main() {
int pp = 1;
int* p = &pp;
sizeof (int) * p;
}
I use GCC to compile it and get the following error:
main.c:4: invalid operands to binary *
So compiler takes "*" as multiplication.
Do you see at what point I am wrong?