Re: Technical reason why sizeof() is an operator and not a function?
- From: Gerry Quinn <gerryq@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 17 Jan 2006 19:44:00 -0000
In article <1137503380.079569.41020@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
robert.thorpe@xxxxxxxxxxxx says...
> Gerry Quinn wrote:
> > In article <DNadnUCaEIpbKVbeRVnyrg@xxxxxxxxxxxxxx>, me@xxxxxxxxxxx
> > says...
> > > "Gerry Quinn" <gerryq@xxxxxxxxxxxxxxxxxxx> wrote in message
> > > news:MPG.1e35b6ab4dfab6fd98a9a5@xxxxxxxxxxxxxxxxxxx
> > > > In article <isednWzN_-QSeFfeRVn-vg@xxxxxxxxxxx>,
> > > > gershwin@xxxxxxxxxxxxxxx says...
> > > >
> > > > > Note that it's proper to write sizeof without the parenthesis both when
> > > > > referring to it and when using it on some expression.
> > > > >
> > > > > int* ptr = malloc(100 * sizeof *ptr);
> > > >
> > > > Looks ugly. Why would anyone want to do that?
> > >
> > > It doesn't look ugly to me (although I'd swap the first asterisk and space).
> >
> > That's a C thing.
> >
> > I would write:
> >
> > int* ptr = malloc( 100 * sizeof( int ) );
> >
> > The brackets indicate that sizeof is not an integer,
>
> But how could malloc( 100 * sizeof int ); indicate that sizeof is an
> integer?
The original has an extra asterisk , giving 100 * sizeof * int, which
looks rather like multiplication.
I guess it's a C idiom, though. As somebody pointed out, the use of
the unnatural construction protects against changes in the type of ptr.
But whether that's a win over writing easily readable code is hard to
know.
- Gerry Quinn
.
- Follow-Ups:
- Re: Technical reason why sizeof() is an operator and not a function?
- From: Rob Thorpe
- Re: Technical reason why sizeof() is an operator and not a function?
- From: Arthur J. O'Dwyer
- Re: Technical reason why sizeof() is an operator and not a function?
- References:
- Technical reason why sizeof() is an operator and not a function?
- From: Chad
- Re: Technical reason why sizeof() is an operator and not a function?
- From: Peter Ammon
- Re: Technical reason why sizeof() is an operator and not a function?
- From: Gerry Quinn
- Re: Technical reason why sizeof() is an operator and not a function?
- From: Alex Fraser
- Re: Technical reason why sizeof() is an operator and not a function?
- From: Gerry Quinn
- Re: Technical reason why sizeof() is an operator and not a function?
- From: Rob Thorpe
- Technical reason why sizeof() is an operator and not a function?
- Prev by Date: Newsgarbage user-controlled tech news site goes into beta!
- Next by Date: Re: C program to find RAM size?
- Previous by thread: Re: Technical reason why sizeof() is an operator and not a function?
- Next by thread: Re: Technical reason why sizeof() is an operator and not a function?
- Index(es):
Relevant Pages
|