Skip to content

Instantly share code, notes, and snippets.

@divanvisagie
Last active December 13, 2015 19:28
Show Gist options
  • Save divanvisagie/4962658 to your computer and use it in GitHub Desktop.
Save divanvisagie/4962658 to your computer and use it in GitHub Desktop.
An attempt to explain pointers
#include <stdio.h>
/* NOTE:
arr = refers to the array as a whole and/or the pointer to the first element of the array
*arr = first element of an array
*/
int main( int argc, char** argv ){
int arr[] = { 1,7,5,6 };
int* ptr = arr; /* pointer to the first element of arr*/
do{ /* we iterate until ptr is the same as the pointer to the last element in the array*/
printf( "%d\n", *ptr ); /* we print the value associated with the pointer */
} while( ptr++ != arr+((sizeof(arr)/sizeof(*arr))-1) );
/*
This for loop does the same thing the do{...} while did
*/
for ( int* iter = arr; iter != arr+( sizeof(arr)/sizeof(*arr) ); iter++ ){
printf( "for: %d\n", *iter );
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment