Associativity of unary C Operators

B

Ben Pfaff

dspfun said:
void *p;
printf("%i\",sizeof(*p));

This prints 1, why is the size of a dereferenced void pointer 1?

Probably because you're using GCC in nonconforming mode.

From the GCC manual:

5.17 Arithmetic on `void'- and Function-Pointers
================================================

In GNU C, addition and subtraction operations are supported on pointers
to `void' and on pointers to functions. This is done by treating the
size of a `void' or of a function as 1.

A consequence of this is that `sizeof' is also allowed on `void' and
on function types, and returns 1.

The option `-Wpointer-arith' requests a warning if these extensions
are used.
 
R

Richard Heathfield

dspfun said:

void *p;
printf("%i\",sizeof(*p));

This prints 1, why is the size of a dereferenced void pointer 1?

It's a constraint violation requiring a diagnostic message from your
compiler. The compiler is free to produce executable code nonetheless, but
if it does so, the Standard does not dictate what the code will do.
 
R

Richard Heathfield

dspfun said:

printf("%i\n",sizeof(void));

This prints 1, so I guess the size of the void type is 1 byte.

You guess wrong. Turn up the warning level on your compiler.
 
K

Keith Thompson

dspfun said:
printf("%i\n",sizeof(void));

This prints 1, so I guess the size of the void type is 1 byte.

Let me guess, you're using gcc, right?

In standard C, "sizeof(void)" is a constraint violation; gcc should
report this if you use the right command-line options.

gcc, as an extension, supports arithmetic on void*. The authors
decided the most straightforward way to do this was to treat type void
as having a size of 1 bytes. (IMHO, this was a poor decision.)
 
C

CBFalconer

dspfun said:
Peter "Shaggy" Haywood skrev:
.... snip ...

void *p;
printf("%i\",sizeof(*p));

This prints 1, why is the size of a dereferenced void pointer 1?

You are probably using gcc, without the necessary -ansi -pedantic
flags. By default gcc compiles a not-C language. In that language
a *void is treated as a char.
 
R

Richard Heathfield

jacob navia said:
CBFalconer a écrit :

Can't you be less drastic Chuck?

It's hardly drastic. It's merely accurate.
ALl compilers have extensions.

None of which are topical here, since this newsgroup discusses the C
language, and specific extensions are not part of the language.
 
C

Cesar Rabak

jacob navia escreveu:
CBFalconer a écrit :

Can't you be less drastic Chuck?

ALl compilers have extensions.
gcc is drastic as well, isn't it? To get near to Standard conformance
the switch is '-pedantic'. . .
 
C

CBFalconer

Cesar said:
jacob navia escreveu:

gcc is drastic as well, isn't it? To get near to Standard
conformance the switch is '-pedantic'. . .

As I said in my original post. I wish people would snip in
paragraph units.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top