Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,821 answers

573 users

How to write a macro that get array size of any data type in C

2 Answers

0 votes
#include <stdio.h>

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

int main(int argc, char **argv)
{
	int intArr[7] = {1, 2, 3, 4, 5};
	char *stringArr[9] = {"c", "c++", "java", "php"};
	float floatArr[5] = {3.14, 5.68};

	printf("%I64d\n", ASIZE(intArr));
	printf("%I64d\n", ASIZE(stringArr));
	printf("%I64d\n", ASIZE(floatArr));

    return 0;
}

/*
run:

7
9
7

*/

 



answered Dec 22, 2018 by avibootz
edited Dec 23, 2018 by avibootz
0 votes
#include <stdio.h>

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

int main(int argc, char **argv)
{
	int intArr[] = {1, 2, 3, 4, 5};
	char *stringArr[] = {"c", "c++", "java", "php"};
	float floatArr[] = {3.14, 5.68};

	printf("%I64d\n", ASIZE(intArr));
	printf("%I64d\n", ASIZE(stringArr));
	printf("%I64d\n", ASIZE(floatArr));

    return 0;
}

/*
run:

5
4
2

*/

 



answered Dec 22, 2018 by avibootz
edited Dec 23, 2018 by avibootz

Related questions

3 answers 116 views
1 answer 160 views
1 answer 211 views
1 answer 235 views
1 answer 162 views
1 answer 161 views
...