Groups | Search | Server Info | Login | Register


Groups > comp.lang.c > #397443

Re: pedantic gcc and const 2D arrays

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: pedantic gcc and const 2D arrays
Date 2026-04-08 21:42 -0700
Organization A noiseless patient Spider
Message-ID <86ldewwxqp.fsf@linuxsc.com> (permalink)
References <20260409012107.00006dc5@yahoo.com> <87ldex2h8e.fsf@example.invalid> <20260409030937.00000c95@yahoo.com>

Show all headers | View raw


Michael S <already5chosen@yahoo.com> writes:

> On Wed, 08 Apr 2026 15:57:05 -0700
> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>
>> Michael S <already5chosen@yahoo.com> writes:
>>
>>> IIRC, this is my first on-topic post in comp.lang.c group.
>>> As they say, nobody is perfect.
>>>
>>> // pedant_2d.c
>>> int bar(const int a[5][42])
>>> {
>>>   return a[0][0];
>>> }
>>>
>>> int foo(int x)
>>> {
>>>   int a[5][42];
>>>   a[0][0] = 1;
>>>   return bar(a);
>>> }
>>>
>>> // end of pedant_2d.c
>>>
>>>
>>> $ gcc -std=c17 -pedantic  -c  pedant_2d.c
>>> pedant_2d.c:  In function 'foo':
>>> pedant_2d.c:10:14: warning: invalid use of pointers to arrays with
>>> different qualifiers in ISO C before C23 [-Wpedantic] 10 |   return
>>> bar(a); |              ^
>>>
>>> What does it mean ?
>>
>> This is an incomplete answer.  Perhaps someone else can fill in
>> some more details.
>>
>> Given the parameter declaration `const int a[5][42]`, a is of
>> course a pointer, not an array.  Specifically, it's of type
>> `constint(*)[42])`, or "pointer to array 42 of const int".
>> (The 5 is quietly ignored.)
>>
>> The argument `a` in the call (it might have been clearer to use
>> distinct names) is of type `int[5][42]`, or "array 5 of array
>> 42 of int".  In the call, as in most contexts, it decays to
>> `int (*)[42]`, or "pointer to array 42 of int".
>>
>> The problem is that the types "pointer to array N of int" and
>> "pointer to array N of const int" are distinct and are not
>> assignment-compatible.
>
> int bar(const int a[42])
> {
>   return a[0];
> }
>
> int foo(int x)
> {
>   int a[42];
>   a[0] = 1;
>   return bar(a);
> }
>
> That, of course, compile with no wraning.
> So, 'pointer to int' is, according to gcc, assignment-compatible (on
> the right side) with 'pointer to const int', but in case of pointers to
> arrays it is not the same?

The analogous case with pointers is not const int * but const int **,
which also fails if given an int **.  Andrey Tarasevich gave an
explanation.

> IMHO, if the standard really says that then every reasonable compiler
> shall ignore the Standard and follow practicality.

I think that's an overreaction.  This particular problem doesn't come
up very often.  When it does, usually it's easy to fix just by writing
a wrapper function and calling that instead:

    int bar(const int a[5][42])
    {
      return a[0][0];
    }

    int bas(int a[5][42]){
      return bar( (const int (*)[42]) a );
    }

    int foo(int x)
    {
      int a[5][42];
      a[0][0] = 1;
      return bas(a);
    }

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

pedantic gcc and const 2D arrays Michael S <already5chosen@yahoo.com> - 2026-04-09 01:21 +0300
  Re: pedantic gcc and const 2D arrays Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-04-08 15:57 -0700
    Re: pedantic gcc and const 2D arrays Michael S <already5chosen@yahoo.com> - 2026-04-09 03:09 +0300
      Re: pedantic gcc and const 2D arrays 🇵🇱Jacek Marcin Jaworski🇵🇱 <jmj@energokod.gda.pl> - 2026-04-09 02:33 +0200
      Re: pedantic gcc and const 2D arrays Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-04-08 21:42 -0700
  Re: pedantic gcc and const 2D arrays Andrey Tarasevich <noone@noone.net> - 2026-04-08 20:34 -0700
    Re: pedantic gcc and const 2D arrays Michael S <already5chosen@yahoo.com> - 2026-04-09 11:09 +0300
    Re: pedantic gcc and const 2D arrays "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-04-09 02:38 -0700
      Re: pedantic gcc and const 2D arrays Michael S <already5chosen@yahoo.com> - 2026-04-09 14:06 +0300
        Re: pedantic gcc and const 2D arrays "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-04-09 13:09 -0700
          Re: pedantic gcc and const 2D arrays Andrey Tarasevich <noone@noone.net> - 2026-04-12 11:30 -0700
            Re: pedantic gcc and const 2D arrays Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-04-13 05:40 -0700

csiph-web