GCC warnings explained

From NetBSD Wiki

Jump to: navigation, search
This article is a stub. You can help by editing it.

This article lists the GCC warnings, the corresponding -W... options, and how to properly fix code that emits these warnings. Apparently, there is no place on the web where the GCC warnings are explained in this style, but there is a need for it.[1]

Contents

Relating the warning messages to command line options

There already is a documentation of the warnings (see #See also), but this documentation cannot be found by entering the warning text into a search engine. You have to know the command line option that is related to the warning.

Warning messages and the related command line options of GCC 4.1.1
Warning message Option
%qE attribute ignored -Wattribute
cast discards qualifiers from pointer target type -Wcast-qual

Specific warnings

array subscript has type `char'

array.c: In function `main':
array.c:35: warning: array subscript has type `char'

Example

char sub;
int arr[50]; // Works

sub = 1;
a[sub] = 5;  // GCC complains

Explanation

The signedness of char is implementation dependent, so using it on a different compiler/system can cause unexpected behaviour.

Solution

Try using unsigned char or int8_t/uint8_t.

cast discards qualifiers from pointer target type

Example code

/* compile with gcc -Wcast-qual */
int
main(void)
{
	const char *hello = "hello, world";
	char *h2 = (char *)hello; /* <-- here */
	return 0;
}

How to prevent this warning

If the variable on the left-hand side is never used to modify anything the pointer points to, add the const qualifier to it and remove the cast:

-	char *h2 = (char *)hello; /* <-- here */
+	const char *h2 = hello;

TODO

See GCC warnings explained/TODO

See also

Personal tools