Pointer Recap Simply put: a pointer is a variable that holds a memory address This gives power to using C that other languages don't offer. To Create a pointer: dataType *pointerName; Pointers need to be defined a specific type so it knows how to interpret the data at the memory address its pointing to. For example, is it pointing to an int that uses 4 bytes? or a char that uses only 1 byte? To debug your program, you can print out the memory address that a pointer is pointing to using %p: int miles; int *ptrMiles; ptrMiles = &miles; // Note: * not used when setting ptr's contents *ptrMiles = 45; // * IS used when setting contents of where ptr points to printf( "Location of miles: %p\n", &miles ); // needs & printf( "Location of ptrMiles: %p\n", ptrMiles ); // no needs & Using char array example #include int main( ) { char string[10] = "Hi there."; char *ptr; ptr = string; // don't need & on string because array is a ptr printf( "at position 4 is %c\n", ptr[4] ); printf( "at position 4 is %c\n", *(ptr+4) ); return 0; } Check for equality int x, y; int *ptrMiles1; int *ptrMiles2; ptrMiles1 = &x; ptrMiles2 = &y; if ( *ptrMiles1 == *ptrMiles2 ) ... Cannot do int *ptrMiles1; *ptrMiles1 = 13; // not pointing to anything yet! Functions Without pointer #include void func( int val ); int main( ) { int xyz = 5; func( xyz ); printf( "%d\n", xyz ); // doesn't change return 0; } void func( int val ) { val = 22; } Functions With pointer #include void func( int *val ); int main( ) { int xyz = 5; func( &xyz ); printf( "%d\n", xyz ); // DOES change return 0; } void func( int *val ) { *val = 22; } Arrays are passed by reference (pointer) automatically to functions Structs are passed by value to functions, so use pointers if you want the function to modify it. Access elements of a struct via a pointer variable: #include struct X { int val; }; int main( ) { struct X stuff = { 77 }; struct X *Y; Y = &stuff; printf( "%d\n", (*Y).val ); printf( "%d\n", Y->val ); }