3

GCC gives a warning when you pass a pointer to an array type to a function which takes a pointer to const array type.

The following code:

void foo (const float (*x) [2])
{
    /* do things */
}

int main()
{
    float a [2];
    foo (&a); /* produces compiler warning with gcc */
    return 0;
}

Produces the warning: "expected 'const float (*)[2]' but argument is of type 'float (*)[2]'"

As far as I am aware, array types are their own separate types and as such you should be able to pass pointers to them in this way. It is not the same problem as passing a "float**" to an function which takes a "const float **". Or is it?

Only gcc seems to give warnings about this, clang is perfectly happy with it.

  • C never automatically introduces (or removes) constness. You need to cast that pointer. –  Jul 09 '16 at 13:26
  • What @Rhymoid; this has nothing to do with your type being an array – you're simply not passing something that is the same type. – Marcus Müller Jul 09 '16 at 13:27
  • Remove the `const`? – GramThanos Jul 09 '16 at 13:29
  • In this case the warning isn't really necessary to prevent type abuse, since assigning to arrays is not possible, but it is still correct. – 2501 Jul 09 '16 at 14:08
  • The const there is fine. From http://en.cppreference.com/w/c/language/conversion: "A pointer to an unqualified type may be implicitly converted to the pointer to qualified version of that type (in other words, const, volatile, and restrict qualifiers can be added. The original pointer and the result compare equal." – Flying_Pigeon Jul 09 '16 at 14:09
  • @Flying_Pigeon Please read: http://c-faq.com/ansi/constmismatch.html – 2501 Jul 09 '16 at 14:15

0 Answers0