Re: void * vs char *
- From: Francine.Neary@xxxxxxxxxxxxxx
- Date: 19 Mar 2007 11:30:43 -0700
On Mar 19, 10:39 am, Ian Collins <ian-n...@xxxxxxxxxxx> wrote:
Francine.Ne...@xxxxxxxxxxxxxx wrote:
On Mar 19, 12:11 am, Ian Collins <ian-n...@xxxxxxxxxxx> wrote:
To avoid casting every assignment of something other than a char* to and
from a char*.
I don't really follow this argument about minimizing number of casts.
For example, the following code fails - I still need to explicitly
cast p to a (struct s*) to avoid a compile-time error.
No, you don't.
#include <stdio.h>
Let's start by making it legal C:
void call(void *);
struct s { int a; };
main()int main(void)
???
main() is perfectly valid C - it defines main to be a function taking
no arguments and returning int. You may have a personal preference for
the more verbose form, but that doesn't make it invalid. I've borrowed
a copy of the ANSI Standard from the library, so now I too can quote
chapter & verse :)
If the expression that precedes the parenthesized argument list in aFrom 6.3.2.2:
function call consists solely of an identifier, and if no declaration
is visible for this identifier, the identifier is implicitly declared
exactly as if, in the innermost block containing the function call,
the declaration
extern int identifier();
appeared.
and from 6.5.4.3:
An empty list in a function declarator that is part of a definition of
that function specifies that the function has no parameters.
(For reference, it continues: The empty list in a function declarator
that is not part of
a definition of that function specifies that no information about the
number or types of the parameters is supplied.)
void call(void *p)
{
printf("%d\n",p->a);
struct s *ps = p;
A rose by any other name... I don't see that this gains anything in
terms of clarity or brevity over "printf("%d\n",((struct s*)p)->a);" -
in fact it just leads to an additional pointer floating around.
Now change call to
void call( char* );
And see what your compiler has to say. If you don't get two
incompatible type warnings, you haven't turned your warning level high
enough.
As this conversion is lossless, I don't see why the compiler should
take it upon itself to warn me about it!
--
Ian Collins.
.
- Follow-Ups:
- Re: void * vs char *
- From: Stephen Sprunk
- Re: void * vs char *
- From: Ian Collins
- Re: void * vs char *
- From: Richard Heathfield
- Re: void * vs char *
- From: Keith Thompson
- Re: void * vs char *
- References:
- void * vs char *
- From: Francine . Neary
- Re: void * vs char *
- From: Ian Collins
- Re: void * vs char *
- From: Francine . Neary
- Re: void * vs char *
- From: Ian Collins
- void * vs char *
- Prev by Date: Re: help me to write a base conversion program
- Next by Date: Re: Little problem with a delirant program
- Previous by thread: Re: void * vs char *
- Next by thread: Re: void * vs char *
- Index(es):
Relevant Pages
|