Created
August 25, 2024 08:09
-
-
Save mortymacs/0f2b7f68bc5ac9baeead032e7e17ae9d to your computer and use it in GitHub Desktop.
C Pointer playground
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
#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