Skip to content

Instantly share code, notes, and snippets.

@Scrappers-glitch
Created January 26, 2023 22:09
Show Gist options
  • Save Scrappers-glitch/bf16a68ac7c7e081a05869a2396d24e9 to your computer and use it in GitHub Desktop.
Save Scrappers-glitch/bf16a68ac7c7e081a05869a2396d24e9 to your computer and use it in GitHub Desktop.
Pass by reference V.S. Pass by address
#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