"srivatsan_b" <> writes:
> 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?
Assuming that you've remembered to #include <string.h>, which declares
the memset() function, the cast is unnecessary. Any object pointer
type can be implicitly converted to void*, and vice versa.
There are a couple of problems with your code, one serious and one
cosmetic.
Given a declaration of "struct some_structure", there is no type
called "some_structure"; the struct keyword is necessary unless you've
also declared it as a typedef. (Some would argue that typedefs for
struct types are poor style.)
<OT>
Note that the rules are different for C++. If I recall correctly, a
declaration of "struct some_structure" makes "some_structure" visible
as a type name, and there is no implicit conversion to void*. That
shouldn't be relevant unless you're using a C++ compiler -- and if you
are, you're in the wrong newsgroup.
</OT>
Also, using the size of the object makes for cleaner code than using
the size of the type. For example:
struct some_structure x;
memset(&x, 0, sizeof x);
Not only is this shorter, it avoid errors if the type of x is changed.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.