Re: void * vs char *



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 :)

From 6.3.2.2:
If the expression that precedes the parenthesized argument list in a
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.


.



Relevant Pages

  • Re: How to pass a struct to a function
    ... write an expression evaluating to your struct -- the name of a variable ... you haven't declared a type "struct entry". ... about how this declaration is handled, ... on which compiler you're using, but any decent compiler will print ...
    (comp.lang.c)
  • Re: Confusion in ANSI Cs function concepts
    ... is no function declaration corresponding to the function call and the ... should assume a declaration with an int return type. ... the compiler will assume that fun is a function returning int ... in the above case the return type is mentioned as void *. ...
    (comp.lang.c)
  • Re: syntax errror
    ... >>> int maxGrey' ... >> declaration of maxGrey. ... typedef double; ... struct some_tag_name { ...
    (comp.lang.c)
  • Re: It Pays to Enrich Your C Skills
    ... Check if you can score a perfect 10 (without using a compiler). ... int main{ ... struct bitfield { ... out if it is a negative integer constant or a constant expression ...
    (comp.lang.c.moderated)
  • Re: Passing an already declarted array of structures!
    ... void LCD_PAINTSCREEN ... in above code by "int", ... If you guys see nothing wrong with the code, then I suppose its a compiler ... struct S { ...
    (microsoft.public.vc.language)