-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcast.c
41 lines (37 loc) · 901 Bytes
/
cast.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
$ clang cast.c -o cast && ./cast
$ clang --version
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix
>cl cast.c && cast.exe
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
cast.c
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:cast.exe
cast.obj
*/
#include <stdio.h>
int main(int argc, char** argv) {
struct V {
char v;
};
struct S {
struct V* v;
};
struct S s;
struct V v;
struct S* p = &s;
v.v = 0;
s.v = &v;
size_t result = sizeof(*((struct S*)p->v)); //cast与->的优先级
if (result == sizeof(*(((struct S*)p)->v))) {
printf("cast");
}
else if (result == sizeof(*((struct S*)(p->v)))) {
printf("->");
}
return 0;
}