tags:

views:

510

answers:

2

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?

==================================================

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.

+2  A: 

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
Sorry ..its basically part of netbsd..I cannot post the program.gcc version :arm--netbsdelf-gcc -vUsing built-in specs.Target: arm--netbsdelfThread model: posixgcc version 4.1.2 20061021 (prerelease) (NetBSD nb3 20061125)
kumar
my mistake, it is there as part of a macro check which is not defined.:(
kumar
+2  A: 

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