Re: size of pointer variables



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

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
.



Relevant Pages

  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)
  • Re: [RFC] timers, pointers to functions and type safety
    ... * they have callback of type void ... callback is called by the code that even in theory has no ... cast to unsigned long and cast back in the callback. ... number - not a pointer cast to unsigned long, not an index in array, etc. ...
    (Linux-Kernel)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... You can't do pointer arithmetic on a void* value. ... qsort behaves in a manner consistent with its specification. ... actually works as advertised...but doesn't mean that the cast is ...
    (comp.lang.c)
  • Re: The void** pointer breaking symmetry?
    ... void** is a generic pointer type that can be implicitly converted ... because dereferencing the void ** variable once gives ...
    (comp.lang.c)
  • Re: Maps, filters and accumulators
    ... void pointer they get back to a pointer to the appropriate type of data. ... int(*compar)(const void *, const void *)) ... avoid having to do all that work (either by getting a code generator to ...
    (comp.lang.c)