Categories
c programming tips and tricks

Macro to Get Array Size of Any Data Type

The following macro will help you in getting the size of an array of any data type.  It works by dividing the length of the array to the size of its field.

#define NUM_OF(x) (sizeof (x) / sizeof (*x))

#define num(x) (sizeof (x) / sizeof (*x))

int _tmain(){

	int number[10] = {1,1,1,1,1,1};
	char *teststr[20] = {"","","","","","","","",""};

	printf("Size of number[10] is %d\n", num(number));
	printf("Size of teststr[20] is %d\n", num(teststr));
}
Size of number[10] is 10
Size of teststr[20] is 20
Press any key to continue . . .

Leave a Reply

Your email address will not be published. Required fields are marked *