Re: size of pointer variables





Richard Bos wrote:
> junky_fellow@xxxxxxxxxxx wrote:
>
> > typedef struct node {
> > long l;
> > int i ;
> > short s1;
> > short s2;
> > char c;
> > }node_t;
> >
> > node_t * pstr_node;
> > bzero(((char *)pstr_node, sizeof (*pstr_node));
> >
> > Is this legal ? Can we typecast structure pointer to char pointer ?
>
> The code is not legal, for a couple of reasons. First, bzero() is not an
> ISO C function. Second, you don't initialise pstr_node, so it points
> nowhere when you pass it to bzero(). Third, you have a parenthesis too
> many (and two superfluous, but harmless ones. BTW, I disagree with
> bjrnove on using sizeof (node_t); the way you have it is more solid.).
>
Can you please specify why sizeof(*pstr_node) is better as compared
to sizeof(struct node_t) ? I thought they both are equivalent.

> The cast in itself, though, is legal; everything can be cast to a char
> *, which must have the same size and representation as a void *, so like
> a void *, it can represent all other kinds of pointers.
> It is probably superfluous, though, since the most usual implementations
> of bzero() take a void * as their first argument, and you can pass any
> object pointer as a parameter declared as void *, without a cast.
>
> Richard

That means pointer variable to any type may be typecasted to (char *)
but *not* the viceversa ?

.



Relevant Pages