Re: memset doubt



On 30 Aug 2005 22:43:54 -0700, "srivatsan_b" <srivatsanbs@xxxxxxxxx>
wrote:

>Hi,
>
>Can somebody explain whether an explicit typecast is mandatory while
>calling memset function for a structure? like in the following code
>snapshot.....
> struct some_structure x;
> memset((some_structure*)&x,0,sizeof(some_structure));
>Will memset(&x,0,sizeof(some_structure)); cause some issues?
>

Firstly, &x already has type pointer to struct some_structure.
Therefore, casting any value to the same type it already has is by
definition redundant.

Secondly, memset expects the first argument to have type pointer to
void. If you were going to cast the value, that is what you would
cast it to.

Thirdly, pointer to void is "compatible" with any other unqualified
object pointer type. This means that the compiler can implicitly
convert one to the other whenever it needs to without requiring a
cast.

The only problem with your final question is that for C89 compilers
the cast must include the keyword struct. The use of the structure
tag without the keyword did not become standard until C99 and there
are very few compilers of that grade in use.


<<Remove the del for email>>
.



Relevant Pages

  • Re: reinterpret_cast
    ... types were declared using the class keyword or the struct keyword. ... in such cases the C cast is always equivalent to static_cast (which will ... can be performed using the cast notation of explicit type conversion. ... --a pointer to an object of derived class type or an lvalue of derived class ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Cast to a struct?
    ... Neither the source expression nor the target type of a cast is ... allowed to be a struct (except that any expression can be cast to ... A cast specifies a type conversion. ... unsigned int is exactly 32 bits. ...
    (comp.lang.c)
  • Re: Linked List Library
    ... I don't recommend casting the return value of malloc: ... The cast is not required in ANSI C. ... When calling malloc(), I recommend using the sizeof operator on ... typedef struct ADCLIST_el ...
    (comp.lang.c)
  • Re: [PATCH] misc: Add oqo-wmi driver for model 2 OQO backlight and rfkill control
    ... +static struct oqo_settings orig, curr; ... Unneeded and undesirable cast of void*. ... +static int smread_u8 ... This driver does quite a lot of casting of things which the compiler ...
    (Linux-Kernel)
  • Re: Casting struct[] to object[]
    ... conversion from struct to object isn't a cast. ... System.Array or System.Object, you want to cast every item in the array. ... > private int TestA; ...
    (microsoft.public.dotnet.languages.csharp)