0

I'm trying to understand how to write a macro in C that will determine if an element is an array or a pointer (only these two options) and return true if it is a big array, false if a pointer. This is what I have:

#define IS_ARRAY(vec) { (sizeof(vec) != sizeof(*void)) ? true : false; }/

Is it correct?

SpaceNugget
  • 105
  • 7
  • How would it tell between an 8-byte character array and an 8-byte pointer? – stark Mar 15 '22 at 20:40
  • As far as I'm aware there is no way to differentiate between an array and a pointer since the "value" of the array is the address of the first element. At the very least checking the size of the value will not help you. – joshmeranda Mar 15 '22 at 20:44
  • after fixing your macro, [not quite](https://godbolt.org/z/r7eY6858h) – yano Mar 15 '22 at 20:44
  • 1
    @joshmeranda: The value of an array is an aggregate containing the values of its elements. An array is not a pointer. – Eric Postpischil Mar 15 '22 at 20:51
  • @EricPostpischil Thanks for filling in a knowledge gap. I've rarely had to deal with a full array rather than just accessing elements. In case anyone stumbles upon this here is an article that I think does a decent job at covering the differences: [Basics of Pointers and Arrays in C](https://denniskubes.com/2012/08/19/pointers-and-arrays-in-c//) – joshmeranda Mar 15 '22 at 21:06

0 Answers0