Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Associativity of unary C Operators

Reply
Thread Tools

Associativity of unary C Operators

 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-03-2007
"dspfun" <(E-Mail Removed)> writes:

> 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.


--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      01-03-2007
dspfun said:

<snip>

> 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      01-03-2007
dspfun said:

<snip>

> 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-03-2007
"dspfun" <(E-Mail Removed)> writes:
[...]
> 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.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-04-2007
dspfun wrote:
> Peter "Shaggy" Haywood skrev:
>

.... snip ...
>>
>> #include <stdlib.h>
>> some_type *ptr;
>> ptr = malloc(sizeof *ptr);
>>
>> This is fine, even though the pointer has not been initialised
>> before calling malloc(). sizeof's operand is not evaluated, except
>> to determine the size, so the dereference is not actually performed.

>
> 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      01-04-2007
CBFalconer a écrit :
> By default gcc compiles a not-C language.


Can't you be less drastic Chuck?

ALl compilers have extensions.


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-04-2007
jacob navia said:

> CBFalconer a écrit :
>> By default gcc compiles a not-C language.

>
> 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Cesar Rabak
Guest
Posts: n/a
 
      01-04-2007
jacob navia escreveu:
> CBFalconer a écrit :
>> By default gcc compiles a not-C language.

>
> 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'. . .
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-05-2007
Cesar Rabak wrote:
> jacob navia escreveu:
>> CBFalconer a écrit :

>
>>> By default gcc compiles a not-C language.

>>
>> 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'. . .


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

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
associativity of operators subramanian100in@yahoo.com, India C Programming 8 03-18-2007 03:35 AM
( ) -> operators associativity confused bochengnever@gmail.com C Programming 2 10-25-2006 02:15 AM
Associativity of unary operators Ashwin N Java 4 09-09-2006 12:59 PM
Ordinary unary function to STL adaptable unary predicate how? SpOiLeR C++ 10 10-19-2005 01:18 PM
Template friend (unary and binary) operators Ruben Campos C++ 5 11-30-2004 05:07 PM



Advertisments