0

In C language macro for getting number of array elements is well known and looks like this:

uint32_t buffer[10];

#define ARRAY_SIZE(x)     sizeof(x)/sizeof(x[0])

size_t size = ARRAY_SIZE(buffer);

The question is, is there any universal macro for getting array elements or returning just one when it's used on variable ? I mean following usage of macro :

uint32_t buffer[10];
uint32_t variable;

#define GET_ELEMENTS(x)     ???

size_t elements_of_array = GET_ELEMENTS(buffer); // returns 10
size_t elements_of_variable = GET_ELEMENTS(variable); // returns 1

Does anybody know the solution ?

I've edited my question because it was wrongly formulated, BTW I know that I can use :

sizeof(variable)/sizeof(uint32_t)

Question is how to combine it in one macro or maybe inline function is better solution ?

Lazureus
  • 462
  • 4
  • 19

1 Answers1

3

You could use sizeof, like this:

#include <stdio.h>

int main(void)
{
  unsigned int buffer[10];
  // size of one unsigned int
  printf("%zu\n", sizeof(unsigned int));

  // this will print the number of elements of buffer * size of an unsigned int
  printf("%zu\n", sizeof(buffer));

  // you have a mistake
  // the following printf() gives you the number of elements
  printf("%d\n", sizeof(buffer)/sizeof(buffer[0]));
  return 0;
}

Output (on my machine):

4
40
10

Regarding the edit you made:

You can't perform type checking in C, thus you can not check if what you pass is an array or a variable.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • You're right I got a mistake there, I've edited my question it should be now more meaningful question. – Lazureus Oct 01 '14 at 12:49
  • 1
    @Lazureus: It's now a different question, which kind of invalidates all comments and answers up to now. – M Oehm Oct 01 '14 at 12:56
  • Thank you for answer, unfortunately I've already edited my question here, I don't want to make additional mess, if you got proper answer for edited question please post it, I would be very grateful. – Lazureus Oct 01 '14 at 13:04
  • As you wish. What exactly do you want now? A macro that performs the division?@Lazureus I mean I think it's OK the way it is. :) – gsamaras Oct 01 '14 at 13:06
  • Macro which can return number of elements from array or return 1 when variable is passed to it. – Lazureus Oct 01 '14 at 13:07
  • 1
    And how to determine if what we pass in a macro is an array or a variable? This is really a different question. However, I feel that you understood what I answered so far, so I will vote up your question for balance. @Lazureus – gsamaras Oct 01 '14 at 13:14
  • @Lazureus I am done with the research and I think you can't do what you asked me for in the comment. – gsamaras Oct 01 '14 at 13:33
  • @Lazureus let me say that I didn't take back my upvote. However, I do not know why you are not accepting the answer. Don't you agree with it? – gsamaras Oct 01 '14 at 17:28
  • @G.Samaras Sorry for answering so late but I needed to investigate this link which you gave me. I've tried create this macro basing on some condition statement like follows ```#define GET_ELEMENTS(x) sizeof(x)>sizeof(uint8_t)?ARRAY_SIZE(x):sizeof(x)``` but it cannot be compiled even because macros are evaluated at compile time. Thank you for your time. – Lazureus Oct 02 '14 at 09:27