How can I get the numbers of elements in a macro definition like this:
#define myThings @"A",@"B",@"C",@"D",@"E"
What is the way to get the answer 5 from myThings?
[myThings count] does not work…
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Defines are literal substitution of text: would it work a code like
? If the answer is no, it won’t work, then here it is your problem. You could do
This should work, but I do not suggest to use this “pattern” at all.
ADD
Another way to count how many values there are, could be the following:
this is a C common way to know how many elements are in
arr. Again, I stress the fact thatmyThingsis literally replaced by whatever you defined it using#define, and so the result is the same asAdd2
Note that, if you won’t use
arr“for real”, optimizations will remove it, and there’s no “waste of space”.However, another solution would be to count the number of arguments passed to a macro, so that CountingMacro(myThings) would be replaced with 5. This SO answer might help in this case.