Created
July 21, 2010 03:32
-
-
Save jtbandes/484015 to your computer and use it in GitHub Desktop.
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> | |
void addOne(int num); | |
void addOnePtr(int *num); | |
int main (int argc, const char * argv[]) { | |
int i = 3; // an int | |
printf("i == %d\n", i); | |
printf("&i == %p\n", &i); // reference/address-of operator & | |
int *ip = &i; // a pointer to an int, type (int *) | |
printf("ip == %p\n", ip); | |
printf("*ip == %d\n", *ip); // dereference operator * | |
printf("--------\n" | |
"*ip = 4;\n"); | |
*ip = 4; // dereference operator * | |
printf("ip == %p\n", ip); | |
printf("&i == %p\n", &i); | |
printf("*ip == %d\n", *ip); | |
printf("i == %d\n", i); | |
printf("--------\n"); | |
addOne(i); | |
printf("i == %d\n", i); | |
addOnePtr(&i); | |
printf("i == %d\n", i); | |
return 0; | |
} | |
void addOne(int num) { | |
printf("addOne called -- num: %d (&num: %p)\n", num, &num); | |
num++; | |
printf("addOne: num == %d\n", num); | |
} | |
void addOnePtr(int *num) { | |
printf("addOnePtr called -- num: %p (*num: %d)\n", num, *num); | |
(*num)++; | |
printf("addOnePtr: (num == %p) *num == %d\n", num, *num); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hopefully! I'm thinking of adding a section on arrays as well.