3:pp
is4:pp
is5:pp
.3:*pp
is3:zp
is null.4:*pp
is1:xp
is4:zp
.5:*pp
is2:yp
is5:zp
.4:**pp
isxp
is1
.5:**pp
isyp
is2
.
Last active
August 29, 2015 14:16
-
-
Save strager/c15581719d7cad8ffe7a to your computer and use it in GitHub Desktop.
This file contains 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 x = 1; | |
int y = 2; | |
int *xp = &x; | |
int *yp = &y; | |
printf("1: xp = %p; *xp = %d\n", xp, *xp); | |
printf("2: yp = %p; *xp = %d\n", yp, *yp); | |
int *zp = NULL; | |
int **pp = &zp; | |
printf("3: pp = %p; *pp = %p; zp = %p\n", pp, *pp, zp); | |
*pp = xp; | |
printf("4: pp = %p; *pp = %p; **pp = %d; zp = %p\n", pp, *pp, **pp, zp); | |
*pp = yp; | |
printf("5: pp = %p; *pp = %p; **pp = %d; zp = %p\n", pp, *pp, **pp, zp); | |
} |
This file contains 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
import ctypes | |
def main(): | |
x = ctypes.c_int(1) | |
y = ctypes.c_int(2) | |
xp = ctypes.pointer(x) | |
yp = ctypes.pointer(y) | |
print('1: xp = {}; *xp = {}'.format(ptr(xp), xp.contents)) | |
print('2: yp = {}; *yp = {}'.format(ptr(yp), yp.contents)) | |
zp = ctypes.POINTER(ctypes.c_int)() | |
pp = ctypes.pointer(zp) | |
print('3: pp = {}; *pp = {}; zp = {}'.format(ptr(pp), ptr(pp.contents), ptr(zp))) | |
pp[0] = xp | |
print('4: pp = {}; *pp = {}; **pp = {}; zp = {}'.format(ptr(pp), ptr(pp.contents), pp.contents.contents, ptr(zp))) | |
pp[0] = yp | |
print('5: pp = {}; *pp = {}; **pp = {}; zp = {}'.format(ptr(pp), ptr(pp.contents), pp.contents.contents, ptr(zp))) | |
def ptr(pointer): | |
'''Format a pointer into a string.''' | |
return ptr1(pointer) | |
#return ptr2(pointer) | |
def ptr1(pointer): | |
address = ctypes.cast(pointer, ctypes.c_void_p).value | |
if address is None: | |
return '(null)' | |
else: | |
return '{:#018x}'.format(address) | |
def ptr2(pointer): | |
address = ctypes.addressof(pointer) | |
if address is None: | |
return '(null)' | |
else: | |
return '{:#018x}'.format(address) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment