Skip to content

Instantly share code, notes, and snippets.

@rhardih
Created October 19, 2015 23:28
Show Gist options
  • Save rhardih/0de0225ba3c91b7fdf9e to your computer and use it in GitHub Desktop.
Save rhardih/0de0225ba3c91b7fdf9e to your computer and use it in GitHub Desktop.
Simple example of manipulating contents of a pointer from within a function call.
#include <stdio.h>
#include <stdlib.h>
int pointer_pointer_function(int **pp)
{
printf("EQUAL WAYS OF ADDRESSING:\n");
printf("*(*pp + 0): %d\n", (int) *(*pp + 0));
printf("*(*pp + 1): %d\n", (int) *(*pp + 1));
printf("*(*pp + 2): %d\n", (int) *(*pp + 2));
printf("(*pp)[0]: %d\n", (int) (*pp)[0]);
printf("(*pp)[1]: %d\n", (int) (*pp)[1]);
printf("(*pp)[2]: %d\n", (int) (*pp)[2]);
printf("\nREALLOCATING TO STORE FOUR INTS:\n");
*pp = realloc(*pp, 4 * sizeof(int));
(*pp)[3] = 576;
return 0;
}
int main (int argc, char const *argv[])
{
int *p = malloc(3 * sizeof(int));;
p[0] = 5;
p[1] = 15;
p[2] = 28;
printf("p[0]: %d\n", p[0]);
printf("p[1]: %d\n", p[1]);
printf("p[2]: %d\n", p[2]);
printf("\nREADING OF UNALLOCATED MEMORY:\n");
printf("p[3]: %d\n\n", p[3]);
pointer_pointer_function(&p);
printf("p[0]: %d\n", p[0]);
printf("p[1]: %d\n", p[1]);
printf("p[2]: %d\n", p[2]);
printf("p[3]: %d\n\n", p[3]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment