[cfe-dev] The %p conversion and cast to void*

Rouben Rostamian rostamian at umbc.edu
Fri Feb 20 19:56:58 PST 2015


Let me begin by confessing that I am not a Clang user -- I use gcc
since that's what comes with my Linux distribution by default.

I teach a course which involves some programming in C.  Some of
my students do use Clang.  The following issue involving Clang
was brought to me by them and I have no explanation for it.
I will pass any helpful comments that I receive here to them
for their information.  Therefore, I thank you in advance.

Consider the following C program:

---file: tryme.c --------------------------------------
#include <stdio.h>
int main(void)
{
	int n;
	printf("%p\n", (void *)&n);
	return 0;
}
-------------------------------------------------------

This compiles cleanly with Clang (on a MacBook) and GNU C (on Linux):

    cc -Wall -pedantic -std=c99 tryme.c

I believe that the (void *) cast is required, since the
definition of the %p conversion in the C standard calls for
a void pointer for its target.

Removing the cast triggers a warning from GNU C:

    tryme.c: In function ‘main’:
    tryme.c:5:2: warning: format ‘%p’ expects argument of
    type ‘void *’, but argument 2 has type ‘int *’

My question is: Why is it that removing the cast /does not/
trigger a warning in Clang?  Do I need to specify further
compilation flags to get that warning?

Platform: MacBook Pro running OSX Yosemite v 10.10.2
Compiler: 
    % cc --version
    Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
    Target: x86_64-apple-darwin14.1.0
    Thread model: posix

-- 
Rouben Rostamian





More information about the cfe-dev mailing list