Hi Paul,

Paul Eggert <[email protected]> writes:

> On 2025-08-31 15:04, Collin Funk wrote:
>> I always fix those anyways, even
>> though I have personally not used a compiler that does not support the
>> GNU C extension allowing addition to void pointers [1]. Bruno, I am
>> curious, do you know of any compilers where that is not the case?
>
> Not only does every compiler support adding 0 to a null pointer, the
> next version of the C standard will require such support.

Thanks, but I am aware of this. This warning is for something a bit
different. Here is an example (useless) code snippet:

    $ cat example.c 
    void *
    foo (void *buffer, int offset)
    {
      return buffer + offset;
    }    
    int
    main (void)
    {
      return 0;
    }
    $ gcc -Wpedantic example.c
    example.c: In function ‘foo’:
    example.c:4:17: warning: pointer of type ‘void *’ used in arithmetic 
[-Wpointer-arith]
        4 |   return buffer + offset;
          |                 ^

The warning is because it is a GNU C extension to have
"sizeof (void) == 1". This allows you to do arithmitic on void pointers.
The more portable way to write that would be to write:

    void *
    foo (void *buffer, int offset)
    {
      return (char *) buffer + offset;
    }

Collin

Reply via email to