C/Pointers

From Attie's Wiki
Revision as of 23:11, 8 March 2012 by Attie (Talk | contribs)

Jump to: navigation, search

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;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox