Inhibiting the incompatible pointer type specific

2019-05-04 18:55发布

In my code under GCC I get quite many warnings of these two types.

warning: passing argument 1 of 'foo' from incompatible pointer type

and

warning: assignment from incompatible pointer type

I know that normally, these two are serious warnings indicating something really wrong in the code. In my case though wherever these are happening, I know very well what I am doing and I know I can safely ignore these warnings. (And yes I am sure about it)

But now my code has grown to such an extent that these warnings overshadow anything else, in effect hiding potential warnings that could lead me to a nasty bug. So I decided to inhibit them. Soon I realized I don't know how. My gcc version is 4.4.1 and I know I can use:

#pragma GCC diagnostic ignore "-Wname" 

to ignore any warning I want. But from the only big and comprehensive list of GCC warnings I found, I can't seem to find which ones these two are. I came to the point of picking them one by one and turning them into errors to see when the compiling would stop due to the incompatible pointer type, but to no avail like below.

#pragma GCC diagnostic error "-Wimplicit-int"   
#pragma GCC diagnostic error "-Waddress"
#pragma GCC diagnostic error "-Wreturn-type"
           .... //e.t.c.

So.. the question is, does anybody know the name of these warnings so that I can actually inhibit them?

EDIT

Due to the discussion in the comments I have to clarify a misconception that I had which lead me to believe that I had to inhibit the warnings. I thought that there was some cost (no matter how miniscule) involved in performing explicit casts between pointers to structures.

So my thinking, which I now realized was flawed, was that since my program is working and has been working for a long time under different platforms, why add work to just satisfy the compiler?

Well I now realized, and am writing it here in case anyone else stumbles on this topic and has the same line of thinking. There is no cost involved in performing the explicit typecasting of pointers and the advantages are:

  • The compiler stops nagging you with the warnings
  • Other people can know that you did the cast intentionally there and did not make mistakes

So here it is for anyone else who might have the same question.

标签: c gcc warnings
1条回答
爷的心禁止访问
2楼-- · 2019-05-04 19:25

If you use the -fdiagnostics-show-option option, GCC will show you the name of the warning, and you can then disable it.

Disabling the warning is probably not a good idea though: If there is later a legitimate case of this warning, you won't know about it.

You're better off fixing your code to prevent the warning. In your case, this is as easy as adding an explicit pointer cast.

float f = 1.23;
char *a = &f; //warning
char *b = (char*)&f; //no warning

Using the explicit cast will make it clear to anyone reading the code that changing the pointer type was done deliberately.

查看更多
登录 后发表回答