4

I have defined a type like this:

typedef char sType[256];

and a function, with default parameter:

void foo(const sType param = NULL);

MinGW (g++ 4.8.0) compiles it without errors.

Instead, Visual Studio 2015 (Tools 14.0) gives the following error:

error C2040: 'sType': 'int' differs in levels of indirection from 'char [256]'

I tried to cast NULL to const char[], but this leads to:

error C2440: 'type cast': cannot convert from 'int' to 'const char []'

Any hint? Thank you

JeJo
  • 30,635
  • 6
  • 49
  • 88
BillyJoe
  • 878
  • 7
  • 25
  • 1
    try using `nullptr`, instead of `NULL` (also, update your old broken compiler to a modern one http://mingw-w64.org/doku.php). – Clearer May 11 '18 at 07:50
  • I get `error C2065: 'nullptr': undeclared identifier`. Tried [this](https://stackoverflow.com/questions/24433436/compile-error-nullptr-undeclared-identifier) (`nullptr` emulation) but I get other errors. – BillyJoe May 11 '18 at 08:31
  • 1
    You're using an ancient, pre c++11 compiler, so that's no surprise. – Clearer May 11 '18 at 08:39
  • VS2015 should be recent enough to know c++11 and nullptr..... – Federico May 11 '18 at 14:38
  • Seems like a MSVC bug. Does it work if you use `(char *)NULL` instead. (BTW in case you were unaware, `const sType` in this context means `char * const` and not `const char *`) – M.M May 13 '18 at 22:54
  • Tried cast to `char *` or `const char *` but the error is still there ("'int' differs in level of indirection..."). However, it is related to something specific to my project because I tried starting a simple project from scratch and it worked. I wonder if I should cancel this thread... – BillyJoe May 14 '18 at 07:21

1 Answers1

0

Cast NULL not to const char[] (or sType) but to const char*. Function parameters undergo array-to-pointer decay in the very declaration.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • Tried cast to `char *` or `const char *` but the error is still there ("'int' differs in level of indirection..."). However, it is related to something specific to my project because I tried starting a simple project from scratch and it worked. I wonder if I should cancel this thread... – BillyJoe May 14 '18 at 07:25