3

The following function declaration is accepted by gcc, but not accepted by g++.

void do_something(char (*)[]);

The error given by g++ is:

error: parameter '<anonymous>' includes pointer to array of unknown bound 'char []'

I believe that in C, the parameter is converted to char** which is why gcc accepts it fine.

Can I make g++ accept this function somehow?

See example: http://ideone.com/yqvqdB :)

Thanks!

James
  • 97
  • 2
  • 9
  • What is `do_something` actually looking to operate on? Is it looking to operate on a `char**`, or a `char*`? – Zac Howland Dec 13 '13 at 16:10
  • Depends on the compiler you want to use in the long run? Or do you want to use both? – Sash Dec 13 '13 at 16:10
  • 3
    Are you sure that's the exact code you're compiling? That should indeed be equivalent to `char**`, and g++ accepts it: http://ideone.com/njdwyc. A pointer to an array would look like `char (*)[]`. – Mike Seymour Dec 13 '13 at 16:10
  • It's looking to operate on a char**. g++ in the long run :) thanks – James Dec 13 '13 at 16:11
  • 2
    Then why not declare it as a `char**` and be done with it? (Even though what you have is equivalent). – Zac Howland Dec 13 '13 at 16:13
  • Thanks Mike, updated my question to show the correct syntax I'm using. – James Dec 13 '13 at 16:13
  • `void do_something (std::vector& strings)` would be a nice way to get this working in C++. – hetepeperfan Dec 13 '13 at 16:22
  • http://ideone.com/WNwkYj Doesn't appear g++ accepts that. I don't recall the standard allowing it either, though. – Zac Howland Dec 13 '13 at 16:27
  • Your compiler is trying to allocate memory for the number of `char *`s in the array. – Fiddling Bits Dec 13 '13 at 16:40
  • 2
    Pointer to array of unknown bounds is not allowed as a function parameter in C++11, but the restriction is lifted in C++14. See: https://en.cppreference.com/w/cpp/language/array#Arrays_of_unknown_bound. – MaxPlankton Jan 31 '20 at 02:21

1 Answers1

-1

The GNU GCC compiler uses non-standard compilant to compile program. Add this flag -std=c99 or -std=iso9899:1999 to compile your program as a standard input and you will get an error.

In standard this will be always accepted as a pointer to an array, so you must provide the lenght of the array as it's required for pointers arithmitics.

rullof
  • 7,124
  • 6
  • 27
  • 36