ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

C言語とC++言語コミュのgccの「互換性のないポインタ型」という原因不明の警告

  • mixiチェック
  • このエントリーをはてなブックマークに追加
プログラム(main.c)をgccでコンパイルすると以下の警告がでます。
どういう意味か分かる方おられますか?
gccのバージョンは3.2.2です。


gcc main.c -W -Wall

main.c: 関数 `main' 内:
main.c:9: 警告: 互換性のないポインタ型からの引数 1 個の `func' を渡しますです


プログラムはこうなってます。

void  func( const int a[10][10])
{
}

int  main()
{
    int  a[10][10];

    func( a );

    return 0;
}



よろしくお願いいたします。

コメント(98)

質問文に多少あいまいな部分があるので補足しておきます。

私は今までWindowsのVisual Studio .Netでプログラミング
していて、二次元配列の参照のみ行う際は、質問文のfunc()のように
constを付けて渡していました。
当然その時は警告は出なかった(警告レベルは最大ではなかったが)ので、
このconstの付け方が正しいものと思い込んでいました。

ところがgccでコンパイルしてみると、
動作は正しい(二次元配列の変更はできない)のに警告が出ます。

だからこのconstの付け方は間違っていて、正しい表記方法が
別にあるのだと思ったわけです。

「二次元配列の変更を許さない、正しい関数渡しの表記方法」
が私が一番知りたいことです。
それともキャストしなければ回避できない問題なのでしょうか。

ついでに質問文のconstの付け方はどうしていけないのかも
知りたいことではあります。
規格合致ですね。(const の付け方は正しい)
なぜ、gcc が警告を発するのか理解不能です。

ちなみに以下とした場合は、L1 の行で警告が出るのでしょうか?
それとも L2 の行でしょうか?

void func(const int (*pa)[10])
{
}

int main(void)
{
    int a[10][10];
    int (*p)[10];
    
    p = a;      /* L1 */
    func(p);    /* L2 */
    
    return 0;
}

参考までですが(10がかぶっているので5と10にしています)
void func(const int a[5][10]) は、仮引数として2次元配列 a を
宣言していますが、定義されるのは(型調整により)
要素数10の配列全体のアドレスを格納可能なポインタ変数です。
(定義結果は const int (*a)[10] です)

int main(void)
{
    int a[5][10]; /* 宣言も定義も2次元配列 */
    
    func(a);    /* a は &a[0] です func(&a[0]); と同じです */
                /* 注意: &a でも &a[0][0] でもありません */
    return 0;
}
> 60: Sahmaroさん

ご提示のプログラムでは L2 で警告が出ます。
L1 は大丈夫です。

C言語では以下の3つは同じなのですね。
void  func( int a[10][20] )  // 10は単純に無視される
void  func( int a[][20] )   // 10が無視された結果
void  func( int (*a)[20] )   // 厳密な書き方

そして
void func( int a[][20] )
のような書き方をシンタックスシュガーと言うようですね。
*(a+n)を a[n]と書けることがシンタックスシュガーである、という見解がよくみられますね。
| 2006年06月25日 12:16 61: Kusakabe さん
| > (型調整により)
|
| そんなものはありません。
意外と無知なんですね〜。

JIS X3010:2003(ISO/IEC9899:1999) 6.7.5.3 関数宣言子(関数原型を含む)
から引用です。
 仮引数を“〜型の配列”とする宣言は,“〜型への修飾されたポインタ”に型調整する。
以上引用終わり。
65: Sahmaro
じゃあ、そのISOのほうの原文みたことないわけですね?
| 66: Kusakabe さん
| 「意外と」はありません。
「意外と」の Google 検索結果は、約 3,900,000 件でした。
すでに市民権を得ている感じですね。

| > 仮引数を“〜型の配列”とする宣言は,“〜型への修飾されたポインタ”に型調整する。
| 誤りですね。
なるほど、誤訳という指摘ですか。失礼しました。
正しい翻訳は何になるんでしょうか?
と聞いても提示していただけないでしょうが...
| 67: 焼鳥食べるのが好き。さん
| じゃあ、そのISOのほうの原文みたことないわけですね?
はい、原文は見たことないですよ。
あなたは、この部分の原文を提示できるのでしょうか?
> あなたは、この部分の原文を提示できるのでしょうか?

やきとりさんでなくても提示できると思うが...
6.7.5.3から抜粋します。
7 Adeclaration of a parameter as "array of <i>type</i>" shall be adjusted to "qualified pointer to <i>type</i>", where the type qualifiers (if any) are those specified within the [and] of the array type derivation. If the keyword <b>static</b> also appears within the [and] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as manyelements as specified by the size expression.

<i>...</i>はイタリック、<b>...</b>はボールドです。
http://www.open-std.org/jtc1/sc22/wg14/www/standards/ からWG14 N1124,というリンクを辿るとPDFが無料で取得できます。
上記の部分はpage119(acrobatにとっては131page)です。
ムズー

僕なら構造体か1次元配列にして逃げちゃうなー
それはそれとして、実引数の int [10][10]と仮引数の const int [10][10]って、compatibleなの?
つまり、gccが間違ったwarinigを出してるのか、黙って通すVisualStudio.Netコンパイラの方が間違っているのか。I
ISOの規格書を斜め読みしただけでは分かりませんでした。時間が出来たらじっくり読んでみるけど。
トピックの警告はC89の6.3.2.2に引っ掛かってるみたいだけど・・・
ならどこにconstを付ければKusakabeさんの課題をクリアできるのかが分からない。
> foo(a);
> bar(b);
> biz(c);

bizってのもあるんですね。

"foo bar baz" の検索結果約 274,000 件
"foo bar buz" の検索結果約 79 件
"foo bar biz" の検索結果約 65 件
"foo bar boz" の検索結果約 17 件
2006年06月26日09:30, 77: saitoh:
> http://www.open-std.org/jtc1/sc22/wg14/www/standards/
> からWG14 N1124,というリンクを辿るとPDFが無料で取得できます。

Object not found!
http://www.open-std.org/JTC1/SC22/WG14/www/standards.html
久々に面白いコントをみたよ
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

2006年06月26日 13:08 79: ヒロ(*´д`*)
-----------------------------------------
> 大辞林はぼけすぎ。

書店で買えるへぼじゃない辞書を教えてください。

2006年06月26日 13:10 80: Kusakabe
-----------------------------------------
もじばけ君には、

火炎瓶の数え方まで書いてあるような辞書がよいでしょう。

2006年06月26日 13:14 81: ヒロ(*´д`*)
-----------------------------------------
新明解国語辞典ですか〜
> いいえ。シンタックスシュガーってのはそういうのを言うわけではありません。

そうですか。どうやら誤解していたようですね。
前橋和弥・著「C言語ポインタ完全制覇」p70に記載されてましたので。
本が駄目なのか、本の説明を誤読してただけなのか分かりませんが。。

言語の機能を本質的には増やすわけではないが、プログラマが楽になったりわかりやすくなったりする(追加された)表記法をシンタックスシュガーと言います。

int a[5]={0,1,2,3,4};の5は省略可能ですが、他の何かの代替表現というわけではないので、省略可能であることがシンタックスシュガーではないです。

ただ、使いやすくするための機能を何でもかんでもシンタックスシュガー呼ばわりする向きも一部にあるようで。なげかわしい。
> 77: saitoh さん
情報感謝します。
adjusted だったら「調整」という言葉を使うのがそう不適切とは思えませんね〜。
ISO 規格は有償と思っていましたが、無料で入手できるって驚きです。

> 83: saitoh さん
> それはそれとして、実引数の int [10][10]と仮引数の
> const int [10][10]って、compatibleなの?
この場合、実引数は &a[0] ですから実引数の型は int (*)[10] です。
また、仮引数 const int a[10][10] の定義結果は const int (*a)[10]
ですので仮引数の型は const int (*)[10] です。
したがって、const int (*)[10] の型のポインタ変数を int (*)[10] 型の
ポインタ変数で初期化することに対して型チェックを行い
incompatible pointer type と warning を出しているのではないでしょうか?
単純に初期化で warning を出しているのか、関数呼び出しが関係している
のかは簡単に切り分けができますね。
(って VC 環境しかない私が突っ込んでも仕方ないですけどね。)

> 84: ふぃりす さん
> トピックの警告はC89の6.3.2.2に引っ掛かってるみたいだけど・・・
> ならどこにconstを付ければKusakabeさんの課題をクリアできるのかが分からない。
あれっ?
const の付け方で warning をなくせるとか Kusakabe さん言ってましたっけ?
> const の付け方で warning をなくせるとか Kusakabe さん言ってましたっけ?

警告がなくなるとは言ってなかったと思います。
Splint 3.1.1 --- 02 May 2003

main.c:1:22: Function parameter a declared as manifest array (size constant is
        meaningless)
 A formal parameter is declared as an array with size. The size of the array
 is ignored in this context, since the array formal parameter is treated as a
 pointer. (Use -fixedformalarray to inhibit warning)
main.c: (in function main)
main.c:9:8: Passed storage a not completely defined (*a is undefined): func (a)
 Storage derivable from a parameter, return value or global is not defined.
 Use /*@out@*/ to denote passed or returned storage which need not be defined.
 (Use -compdef to inhibit warning)
$ lint -strict main.c
Splint 3.1.1 --- 02 May 2003

main.c:1:22: Function parameter a declared as manifest array (size constant is
        meaningless)
 A formal parameter is declared as an array with size. The size of the array
 is ignored in this context, since the array formal parameter is treated as a
 pointer. (Use -fixedformalarray to inhibit warning)
main.c: (in function func)
main.c:1:22: Parameter a not used
 A function parameter is not used in the body of the function. If the argument
 is needed for type compatibility or future plans, use /*@unused@*/ in the
 argument declaration. (Use -paramuse to inhibit warning)
main.c:5:5: Function main declared without parameter list
 A function declaration does not have a parameter list. (Use -noparams to
 inhibit warning)
main.c: (in function main)
main.c:9:8: Passed storage a not completely defined (*a is undefined): func (a)
 Storage derivable from a parameter, return value or global is not defined.
 Use /*@out@*/ to denote passed or returned storage which need not be defined.
 (Use -compdef to inhibit warning)
main.c:9:2: Undetected modification possible from call to unconstrained
        function func: func
 An unconstrained function is called in a function body where modifications
 are checked. Since the unconstrained function may modify anything, there may
 be undetected modifications in the checked function. (Use -modunconnomods to
 inhibit warning)
main.c:9:2: Statement has no effect (possible undected modification through
        call to unconstrained function func): func(a)
 Statement has no visible effect --- no values are modified. It may modify
 something through a call to an unconstrained function. (Use -noeffectuncon to
 inhibit warning)
main.c:1:6: Function exported but not used outside main: func
 A declaration is exported, but not used outside this module. Declaration can
 use static qualifier. (Use -exportlocal to inhibit warning)
  main.c:3:2: Definition of func
main.c:1:6: Function func exported but not declared in header file
 A declaration is exported, but does not appear in a header file. (Use
 -exportheader to inhibit warning)
  main.c:3:2: Definition of func

Finished checking --- 8 code warnings

ログインすると、残り75件のコメントが見れるよ

mixiユーザー
ログインしてコメントしよう!

C言語とC++言語 更新情報

C言語とC++言語のメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。