Skip to content

Instantly share code, notes, and snippets.

@luoyetx
Last active March 6, 2016 14:56
Show Gist options
  • Select an option

  • Save luoyetx/7c02f5586e26ed597903 to your computer and use it in GitHub Desktop.

Select an option

Save luoyetx/7c02f5586e26ed597903 to your computer and use it in GitHub Desktop.
learn c points in a weird way
#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;
}
p1[3] = 4
p2[3] = 4
pn[3] = 4
p1[3] = 4
p2[3] = 7
pn[3] = 7
@PaulMurrayCbr
Copy link
Copy Markdown

(assuming a pointer fits into an int on your machine):

pn = (int *************) &pn;

@luoyetx
Copy link
Copy Markdown
Author

luoyetx commented Jul 9, 2015

the program will not crash with pn = (int *************) &pn;. the result is here

p1[3] at 0028FE80 = 4
p2[3] at 0028FE80 = 4
pn[3] at 0028FEB0 = 4233936

you actually get the data pn[3] from stack where the variable pn defined with offset=3_sizeof(int *_***********), It's not from the array data you defined.

@luoyetx
Copy link
Copy Markdown
Author

luoyetx commented Jul 9, 2015

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