Loose-Info.com
Last Update 2021/10/02
TOP - 各種テスト - gcc - 警告関連のオプション - -Wno-discarded-qualifiers

-Wno-discarded-qualifiers
ポインタの型修飾子が破棄された場合でも警告しない

テスト概要

オプション無し、および-Wno-discarded-qualifiersオプションを使用した際の警告出力例

実行環境

GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。

使用ファイル


sample.c
#include <stdio.h> /* 関数の宣言には型修飾子「const」による指定は無し */ void sampFunc(char *str1, char *str2, int *n) { *(str1 + 2) = '\0'; *(str2 + 3) = '\0'; *n = 5; printf("[%s] [%s] [%d]\n", str1, str2, *n); } int main(void) { const char str1[10] = "test"; char str2[10] = "test2"; const int n[3] = {0, 1, 2}; /* str1とnは型修飾子「const」で宣言された変数 */ sampFunc(str1, str2, n); return 0; }

動作テスト

オプション無しでコンパイルを実行
$ gcc sample.c sample.c: In function ‘main’: sample.c:21:11: warning: passing argument 1 of ‘sampFunc’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers] sampFunc(str1, str2, n); ^~~~ sample.c:4:21: note: expected ‘char *’ but argument is of type ‘const char *’ void sampFunc(char *str1, char *str2, int *n) ~~~~~~^~~~ sample.c:21:23: warning: passing argument 3 of ‘sampFunc’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers] sampFunc(str1, str2, n); ^ sample.c:4:44: note: expected ‘int *’ but argument is of type ‘const int *’ void sampFunc(char *str1, char *str2, int *n) ~~~~~^ $ 引数渡しの際にポインタの対象型から'const'修飾子が破棄される事への警告

-Wno-discarded-qualifiersオプション使用してコンパイルを実行
$ gcc -Wno-discarded-qualifiers sample.c エラー・警告無し $