Calculate size without sizeof

Calculate size without sizeof

Calculate size without sizeof 實作教學與筆記。

說明

使用指標來實現。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#define my_sizeof(type) ((char *)(&type+1)-(char*)(&type))

struct test{
int a;
int b;
};

void main(){
int arr[] = {1, 2, 3, 4, 5, 6};
struct test A_struct;

printf("%ld\n", my_sizeof(arr)); // 24
printf("%ld\n", my_sizeof(A_struct)); // 8
}
Author

Meow Lucian

Posted on

2022-07-27

Updated on

2022-07-27

Licensed under

Comments