Skip to content

Instantly share code, notes, and snippets.

@larswaechter
Last active December 30, 2020 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larswaechter/ddc64238803dc30e868b18dbd3ee49f5 to your computer and use it in GitHub Desktop.
Save larswaechter/ddc64238803dc30e868b18dbd3ee49f5 to your computer and use it in GitHub Desktop.
Array pointer arithmetic in C
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