C/Pointers
From Attie's Wiki
(Difference between revisions)
m (Created page with 'A helper for people starting out with pointers: <source lang="c"> #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define P(expr) printf("%10s: 0x%02X\n", #expr, expr)…') |
m |
||
Line 11: | Line 11: | ||
char **p = &s; | char **p = &s; | ||
− | P(s); | + | P( s ); |
− | P(&s); | + | P( &s ); |
− | P(s[0]); | + | P( s[0] ); |
− | P(*s); | + | P( *s ); |
− | P(p); | + | P( p ); |
− | P(&p); | + | P( &p ); |
− | P(p[0]); | + | P( p[0] ); |
− | P(*p); | + | P( *p ); |
− | P(p[1]); | + | P( p[1] ); |
− | P(*p[1]); | + | P( *p[1] ); |
− | P((*p)[1]); | + | P( (*p)[1] ); |
− | P(*(p[1])); | + | P( *(p[1]) ); |
− | + | ||
− | P((*p)); | + | P( (*p) ); |
− | P((*p)++); | + | P( (*p)++ ); |
− | P((*p)[0]); | + | P( (*p)[0] ); |
− | P((*p)); | + | P( (*p) ); |
− | P(++(*p)); | + | P( ++(*p) ); |
− | P((*p)[0]); | + | P( (*p)[0] ); |
− | P((*p)); | + | P( (*p) ); |
return 0; | return 0; | ||
} | } | ||
</source> | </source> |
Revision as of 23:11, 8 March 2012
A helper for people starting out with pointers:
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #define P(expr) printf("%10s: 0x%02X\n", #expr, expr) int main(void) { char *s = "abc"; char **p = &s; P( s ); P( &s ); P( s[0] ); P( *s ); P( p ); P( &p ); P( p[0] ); P( *p ); P( p[1] ); P( *p[1] ); P( (*p)[1] ); P( *(p[1]) ); P( (*p) ); P( (*p)++ ); P( (*p)[0] ); P( (*p) ); P( ++(*p) ); P( (*p)[0] ); P( (*p) ); return 0; }