Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Created August 25, 2024 08:09
Show Gist options
  • Save mortymacs/0f2b7f68bc5ac9baeead032e7e17ae9d to your computer and use it in GitHub Desktop.
Save mortymacs/0f2b7f68bc5ac9baeead032e7e17ae9d to your computer and use it in GitHub Desktop.
C Pointer playground
#include <stdio.h>
#include <stdlib.h>
int *x()
{
int* a = malloc(sizeof(int));
*a = 19;
return a;
}
int main()
{
//define pointer with defined length
int **a = malloc(3*sizeof(int));
//allocating memory for 3 pointers.
*(a) = malloc(sizeof(int));
*(a+1) = malloc(sizeof(int));
*(a+2) = malloc(sizeof(int));
//assign values.
*(a) = x();
**(a+1) = 20;
**(a+2) = 30;
//get output.
printf("%d\n", **(a));
printf("%d\n", **(a+1));
printf("%d\n", **(a+2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment