Last active
March 6, 2016 14:56
-
-
Save luoyetx/7c02f5586e26ed597903 to your computer and use it in GitHub Desktop.
learn c points in a weird way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| int main(int argc, char *argv[]) { | |
| int data[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; | |
| int *p1 = (int *)data; | |
| int **p2 = (int **)data; | |
| int *************pn = (int *************)data; | |
| printf("p1[3] = %d\n", p1[3]); | |
| printf("p2[3] = %d\n", p2[3]); | |
| printf("pn[3] = %d\n", pn[3]); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| p1[3] = 4 | |
| p2[3] = 4 | |
| pn[3] = 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| p1[3] = 4 | |
| p2[3] = 7 | |
| pn[3] = 7 |
Author
Author
the result above is compiled with GCC4.8.1 32bit on Windows.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the program will not crash with
pn = (int *************) &pn;. the result is hereyou actually get the data
pn[3]from stack where the variablepndefined with offset=3_sizeof(int *_***********), It's not from the arraydatayou defined.