Created
January 26, 2023 22:09
-
-
Save pavly-gerges/bf16a68ac7c7e081a05869a2396d24e9 to your computer and use it in GitHub Desktop.
Pass by reference V.S. Pass by address
This file contains hidden or 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> | |
#include <inttypes.h> | |
static inline void printAddress(void** ptr_address) { | |
printf("%p\n", *ptr_address); | |
} | |
static inline void destroy(void** ptr_address) { | |
free(*ptr_address); | |
*ptr_address = NULL; | |
} | |
static inline void printAddress0(void* ptr) { | |
printf("%p\n", ptr); | |
} | |
static inline void destroy0(void* ptr) { | |
free(ptr); | |
ptr = NULL; | |
} | |
int main() { | |
int* ptr = malloc(6 * sizeof(int)); | |
*ptr = 222; | |
printf("%p\n", ptr); | |
destroy(&ptr); | |
printAddress(&ptr); | |
printAddress0(ptr); | |
printf("%i\n", *ptr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment