Created
August 18, 2020 05:54
-
-
Save EvanMu96/f1f56036f969417301a7020aa06cc386 to your computer and use it in GitHub Desktop.
implement a sizeof function
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
// reference | |
// https://cs-fundamentals.com/tech-interview/c/implement-sizeof-operator-in-c.php | |
#include <cstdio> | |
#define MY_SIZEOF(object) (char*)(&object + 1) - (char*)(&object) | |
int main() | |
{ | |
double x; | |
int arr[10]; | |
printf("double var size: %d\n", MY_SIZEOF(x)); | |
printf("double type size: %d\n", sizeof(double)); | |
printf("int arr[10] size : %d\n", MY_SIZEOF(arr)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment