1

I’m using the following flags, but still I m not able to get this warning:

pointer of type void * used in arithmetic

Flags used:

-O2 -Werror -Wall -Wno-main -Wno-format-zero-length -Wpointer-arith -Wmissing-prototypes -Wstrict-prototypes -Wswitch -Wshadow -Wcast-qual -Wwrite-strings -Wno-sign-compare -Wno-pointer-sign -Wno-attributes  -fno-strict-aliasing     

-Wpointer-arith should catch this type of warning, but I’m not able to get this warning:

pointer of type void * used in arithmetic

Which specific cflag should be used to get this warning?

Edit: my mistake, it is there as part of a macro check which is not defined. :( By defining that macro, I’m able to get that error.

David Foerster
  • 1,461
  • 1
  • 14
  • 23
kumar
  • 2,530
  • 6
  • 33
  • 57
  • Which version of gcc are you using? – R Samuel Klatchko Jan 04 '10 at 06:15
  • Can you provide the source code. By just improving the source code, you will be able to clear the warning. – Phong Jan 04 '10 at 06:18
  • Sorry ..its basically part of netbsd..I cannot post the program. gcc version : arm--netbsdelf-gcc -v Using built-in specs. Target: arm--netbsdelf Thread model: posix gcc version 4.1.2 20061021 (prerelease) (NetBSD nb3 20061125) – kumar Jan 04 '10 at 06:32
  • Can you post a *minimal* code example that exhibits what you see? What happens when you compile the program in my answer? – Alok Singhal Jan 04 '10 at 06:40
  • And what about a small code with have the same problem of yours. just put fake variable name, fake function name etc) so it wont be recognizable. – Phong Jan 04 '10 at 06:44
  • Trying to come up with a minimal code example is often very educational. We will unlikely be able to help you if we don't have any code to demonstrate the problem. – JesperE Jan 04 '10 at 07:18
  • my mistake, it is there as part of a macro check which is not defined. :( By defining that macro , I m able to get that error. Thanks for your time...and sorry for my ignorance :( – kumar Jan 04 '10 at 07:56

2 Answers2

2

With gcc 4.2.1 on OS X, I get this warning:

p.c:7: warning: wrong type argument to increment

for the following program:

#include <stdio.h>

int main(void)
{
    int i[] = { 42 };
    void *p = i;
    printf("%p\n", p++);
    return 0;
}

I am compiling it as:

$ gcc -Wpointer-arith p.c

Can you post your program, or post the result of compiling the above?

Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
  • Sorry ..its basically part of netbsd..I cannot post the program. gcc version : arm--netbsdelf-gcc -v Using built-in specs. Target: arm--netbsdelf Thread model: posix gcc version 4.1.2 20061021 (prerelease) (NetBSD nb3 20061125) – kumar Jan 04 '10 at 06:31
  • my mistake, it is there as part of a macro check which is not defined. :( – kumar Jan 04 '10 at 07:21
2

You're right. -Wpointer-arith should give you a warning as per the documentation.

I have just tried the following program (with intentional error):

~/code/samples$ cat foo.c
#include <stdio.h>
int main (int argc, char **argv)
{
  void * bar;
  void * foo;
  foo = bar + 1;
  return 0;
}

I have compiled the program with just the -Wpointer-arith option, and all your options as listed above. Both attempts threw up the desired warning. I am using gcc version 4.3.4 (Debian 4.3.4-6).:

~/code/samples$ gcc -Wpointer-arith foo.c
foo.c: In function ‘main’:
foo.c:6: warning: pointer of type ‘void *’ used in arithmetic

and

~/code/samples$ gcc -O2 -Werror -Wall -Wno-main -Wno-format-zero-length -Wpointer-arith -Wmissing-prototypes -Wstrict-prototypes -Wswitch -Wshadow -Wcast-qual -Wwrite-strings -Wno-sign-compare -Wno-pointer-sign -Wno-attributes -fno-strict-aliasing foo.c
cc1: warnings being treated as errors
foo.c: In function ‘main’:
foo.c:6: error: pointer of type ‘void *’ used in arithmetic

The compiler does throw up the warning if you give it the 'right' code. So, I would recommend you examine why it is you expect this warning. Maybe the code you're compiling has changed?

One possible clue I can give you: foo = bar + 1; in the code above triggers the warning. But foo = bar ++; will not (You get a different warning). So if your code uses increment (or decrement) operators on pointers, it will probably not trigger the warning.

I know this is not a direct answer, but I hope this helps you focus your investigation.

Mike Mytkowski
  • 596
  • 2
  • 8
  • What we really want is a warning on ANY pointer arithmetic using "+". I misread this post and thought that I had found the answer. But pointer arithmetic is part of the soul of C. Of course cerr << "PTR" << ("Yipes" + 1); is valid. – Tuntable Mar 30 '16 at 06:02