LoginSignup
2
2

More than 5 years have passed since last update.

gccの-Wincompatible-pointer-typesオプションめも

Posted at

ある程度汎用的に使いたい時にvoid型のポインタを引数に取ったりするけど、その時にポインタのポインタとかを使う場合は呼び出し側でキャストしてあげないと怒られるよということ。
試したのはgcc 5.2.0。

masami@kerntest:~$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /build/gcc/src/gcc-5.2.0/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --disable-multilib --disable-werror --enable-checking=release --with-default-libstdcxx-abi=gcc4-compatible
Thread model: posix
gcc version 5.2.0 (GCC)

例えばこんなコードがあって、test()はvoid**を引数に取る。そして呼び出し側でtest(&p);とすると-Wincompatible-pointer-typesによってwarningがでる。

#include <stdio.h>

struct foobar {
        int n;
};

void test(void **p)
{
        struct foobar *f = *(struct foobar **) p;
        f->n = 10;
}

int main(int argc, char **argv)
{
        struct foobar f, *p;
        p = &f;
        p->n = 1;

        //test((void **) &p);
        test(&p);

        printf("%d\n", p->n);

        return 0;
}

コンパイル。

masami@kerntest:~$ gcc a.c -Wall -Werror
a.c: In function ‘main’:
a.c:20:7: error: passing argument 1 of ‘test’ from incompatible pointer type [-Werror=incompatible-pointer-types]
  test(&p);
       ^
a.c:7:6: note: expected ‘void **’ but argument is of type ‘struct foobar **’
 void test(void **p)
      ^
cc1: all warnings being treated as errors

では、呼び出し側をとして明示的にキャストしてtest((void **) &p);とした場合。

masami@kerntest:~$ gcc a.c -Wall -Werror
masami@kerntest:~$
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2