0

In the following example, is there a way to raise a compilation error when a pointer is used instead of an array?

#include <stdio.h>

static void foo(int tutu[])
{
    printf("%d",tutu[2]);
}

int main() {
    foo((int[]){1,2,3});//shall display 3 -- displays 3
    foo((int*)(int[]){1,2,3});//would be nice to raise a compilation error -- but displays 3    
    return 0;
}

In other terms, How to forbid usage of pointers (which could points to a not allocated space) and force usage of arrays?

In fact, if I add -Werror to the compilation with gcc, I get

error: taking address of temporary array
   10 |     foo((int[]){1,2,3});

Which is exactly the opposite that I'm looking for...

Guillaume D
  • 2,202
  • 2
  • 10
  • 37
  • Arrays decay to a pointer to their first element, so the function calls are equivalent. What are you trying to accomplish by doing this? – dbush Jul 01 '21 at 13:00
  • Unfortunately, not in C. (this could be done in C++). In C, if you want to make sure what you are getting is an array of fixed size, you'd have to use a pointer to array - but than your function will only accept arrays of a single size, which is usually inconvenient. – SergeyA Jul 01 '21 at 13:02
  • forbid usage of a pointer which could points to a not allocated space – Guillaume D Jul 01 '21 at 13:02
  • Your `foo` function takes a pointer parameter, so it doesn't make sense to warn if using a pointer. – interjay Jul 01 '21 at 13:02
  • @GuillaumeD note, that your function currently DOES take a pointer. `int x[]` in function declaration is a fashionable way to say `int* x`. – SergeyA Jul 01 '21 at 13:04
  • so how to raise a compilation error by differenciating int* and int* const? – Guillaume D Jul 01 '21 at 13:05
  • I'm afraid you have to write you own C compiler or use another language which doesn't support pointer or has the option to disable pointer use. – fpiette Jul 01 '21 at 13:06
  • @GuillaumeD you can not achieve what you hope to achieve, and moreover, it looks like your efforts are misguided. For starters, you do not seem to control size of the array provided - is your function prepared to work with arrays of a fixed size? – SergeyA Jul 01 '21 at 13:07
  • 1
    I think this question may have been asked before (a request for a way to distinguish a pointer and an array, perhaps in different circumstances), and there may be a way to test for an array versus a pointer using `_Generic` and whatnot. You would have to use a macro, as, by the time the function is called, the array has already been converted. – Eric Postpischil Jul 01 '21 at 13:22
  • 1
    [This may be it.](https://stackoverflow.com/questions/19452971/array-size-macro-that-rejects-pointers) The answers there involve compiler extensions or a run-time test. [Here is another question on the topic.](https://stackoverflow.com/questions/16794900/validate-an-argument-is-array-type-in-c-c-pre-processing-macro-on-compile-time/16795563#16795563) – Eric Postpischil Jul 01 '21 at 13:33
  • @EricPostpischil Exactly what I was looking for – Guillaume D Jul 01 '21 at 13:36
  • Note: To make this work in a macro, one can `#define foo(a, b) (whatever test, foo((a), (b)))` or something similar (e.g., GCC’s statement expression `({ whatever test; foo((a), (b)) })`). Because a macro will not be replaced in its own replacement, the `foo((a), (b))` results in a function call rather than a macro invocation. – Eric Postpischil Jul 01 '21 at 13:39

0 Answers0