-1

I would like to create a C macro returning the scalar minimum for any type of static array in input. For example:

float A[100];
int B[10][10];
// [...]
float minA = MACRO_MIN(A);
int minB = MACRO_MIN(B);

How can I do so?

simon.denel
  • 780
  • 6
  • 23
  • 1
    No you can't, and why would you even want to anyway ? Use C++ if you want this kind of functionality. – Paul R Dec 16 '13 at 09:14
  • You would need more than just the array as a parameter of the macro, for example the type and the number of elements. – Eregrith Dec 16 '13 at 09:15
  • 2
    For simple arrays, this should be fairly straight-forward. But I don't think you can do it for multi-dimensional arrays, since you cannot "overload" macros. – Kerrek SB Dec 16 '13 at 09:15
  • Thank you Kerrek, Eregrith. You fully understand my problem: It should be possible for one-dimensional arrays, and the question is, indeed, if it is possible to create such a macro without more parameters. – simon.denel Dec 16 '13 at 09:18
  • 2
    Review [this question on pseudo-generics in C](http://stackoverflow.com/questions/16522341/pseudo-generics-in-c). It has similar goals to your question and some of the answers are illuminating. – Darren Stone Dec 16 '13 at 10:15
  • Thank you very much Darren Stone. Although it is not as clean as I would have dream, it is indeed inspiring and may have no choice but to use such a technique. – simon.denel Dec 16 '13 at 10:26

1 Answers1

1

It can be probably be done with GCC extensions, but not in standard C. Other compilers might have suitable extensions, too. It will of course make the code fantastically hard to port. I would advise against it, since it's quite hard to achieve it will be "unexpected" and probably act as a source of confusion (or, worse, bugs) down the line.

You're going to have to declare a temporary variable to hold the max/min seen "so far" when iterating over the array, and the type of that variable is hard to formulate without extensions.

Also returning the value of the temporary is hard, but possible with GCC extensions.

To make the above more concrete, here's a sketch of what I imagine. I did not test-compile this, so it's very likely to have errors in it:

#define ARRAY_MAX(a)  ({ typeof(a) tmp = a[0];\
                         for(size_t i = 1; i < sizeof a / sizeof tmp; ++i)\
                         {\
                           if(a[i] > tmp)\
                             tmp = a[i];\
                         }\
                         tmp;\
                      })

The above uses:

  • ({ and }) is the GCC Statement Expressions extension, allowing the macro to have a local variable which is used as the "return value".
  • typeof is used to compute the proper type.
  • Note assumption that the array is not of zero size. This should not be a very limiting assumption.

The use of sizeof is of course standard.

As I wrote the above, I realize there might be issues with multi-dimensional arrays that I hadn't realized until trying. I'm not going to polish it further, though. Note that it starts out with "probably".

unwind
  • 391,730
  • 64
  • 469
  • 606
  • How do you distinguish between one-dimensional and multi-dimensional using GCC extension? – BLUEPIXY Dec 16 '13 at 10:15
  • @BLUEPIXY Not sure that you can. But you can probably ignore that (assuming it's a true multidimensional array and not jagged) and use `sizeof` to compute the total number of elements. – unwind Dec 16 '13 at 10:18
  • It's seen in the size of the sizeof the whole it is i know, but, I think that macro need one element type if the multidimensional array is passed. – BLUEPIXY Dec 16 '13 at 10:27
  • probably, Conclusion is not possible. also it has typo. – BLUEPIXY Dec 16 '13 at 11:31
  • @BLUEPIXY Thanks, I think I fixed the typo (the statement expression braces). – unwind Dec 17 '13 at 08:50