Last active
December 30, 2020 23:28
-
-
Save larswaechter/ddc64238803dc30e868b18dbd3ee49f5 to your computer and use it in GitHub Desktop.
Array pointer arithmetic in C
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
int main(int argc, char *argv[]) | |
{ | |
int *arr; | |
arr = malloc(5); | |
arr[0] = 1; | |
arr[1] = 10; | |
arr[2] = 20; | |
arr[3] = 30; | |
arr[4] = 40; | |
printf("address first value: %p\n", arr); | |
printf("first value: %d\n\n", *arr); | |
for (int i = 0; i < 5; i++) | |
{ | |
printf ("arr[%d] address:\t\t%p\n", i, arr + i); | |
printf ("arr[%d+1] address:\t\t%p\n", i, arr + i + 1); | |
printf ("arr[%d+1] address char cast:\t%p\n", i, ((char *) (arr + i)) + 1); | |
printf ("\n"); | |
} | |
free(arr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment