Skip to content

Instantly share code, notes, and snippets.

@sovietspy2
Created February 15, 2026 14:58
Show Gist options
  • Select an option

  • Save sovietspy2/7bfca524066ac1c8dca0361973306b57 to your computer and use it in GitHub Desktop.

Select an option

Save sovietspy2/7bfca524066ac1c8dca0361973306b57 to your computer and use it in GitHub Desktop.
Cookbook for C arrays and array passing to method
#include <stdio.h>
#include <stdlib.h>
/*
* inspect
*
* Demonstrates what int **parr, *parr and **parr mean
* when a pointer is passed by address.
*
* Parameters:
* parr - pointer to a pointer to int (int **)
*
* In a typical caller, parr will point to the caller's
* pointer variable (e.g. &arr), so:
* parr : address of caller's pointer variable
* *parr : the caller's pointer value (array pointer)
* **parr : the first int in the array (arr[0])
*/
void inspect(int **parr)
{
printf("=== inspect() ===\n");
/* address of the local parameter variable 'parr' itself */
printf("&parr (address of parameter 'parr'): %p\n", &parr);
/* value stored in 'parr': address of the caller's pointer variable */
printf("parr (address of caller's pointer): %p\n", parr);
/* value stored in the caller's pointer variable (e.g. 'arr'): heap address */
printf("*parr (pointer to first int in array): %p\n", *parr);
/* the actual first int in the array: (*parr)[0] */
printf("**parr (first int value in the array): %d\n", **parr);
printf("=================\n\n");
}
/*
* main
*
* Sets up a simple example and calls inspect() to show the relationships:
*
* int *arr : pointer to first element of an int array
* &arr : address of that pointer, type int **
*
* Passing &arr into inspect() lets inspect work with:
* parr == &arr
* *parr == arr
* **parr == arr[0]
*/
int main(void)
{
int *arr = malloc(3 * sizeof(int));
if (!arr) {
perror("malloc");
return 1;
}
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
printf("=== main() ===\n");
printf("&arr (address of pointer variable 'arr'): %p\n", (void *)&arr);
printf("arr (pointer to first int in array): %p\n", (void *)arr);
printf("arr[0] (first int value in the array): %d\n", arr[0]);
printf("================\n\n");
/* Pass address of 'arr' so inspect can see & manipulate the pointer itself */
inspect(&arr);
free(arr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment