Last active
December 13, 2015 19:28
-
-
Save divanvisagie/4962658 to your computer and use it in GitHub Desktop.
An attempt to explain pointers
This file contains 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> | |
/* 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