By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,103 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

Structure initialization and clearing

P: n/a
Hi All,

Quick question; what is the difference between initializing and
clearing a structure like this:

mystruct_t a = {0};
and initializing and clearing it like this:

mystruct_t a;

memset(a, 0, sizeof(mystruct_t));

TIA,
Roubles
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Roubles wrote:

Hi All,

Quick question; what is the difference between initializing and
clearing a structure like this:

mystruct_t a = {0};

and initializing and clearing it like this:

mystruct_t a;

memset(a, 0, sizeof(mystruct_t));


In practice, probably not much.

However, suppose the first element of the struct were a pointer, and
you are on a system where a NULL pointer is not all-bits-zero.

Also, my understanding is that _all_ entries will be set to their zero
representation, which may not be all-bits-zero. (For example, a "double"
value.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Nov 14 '05 #2

P: n/a
Roubles wrote:

Hi All,

Quick question; what is the difference between initializing and
clearing a structure like this:

mystruct_t a = {0};
This sets each member of the structure to the equivalent of 0 for the
type. You can only intitialize this way, not assign.
and initializing and clearing it like this:

mystruct_t a;

memset(a, 0, sizeof(mystruct_t));

This sets the whole thing to all-bits 0. For some things like floating
point numbers or pointers, this could be the wrong thing.

Brian Rodenborn
Nov 14 '05 #3

P: n/a
Roubles wrote:
Hi All,

Quick question; what is the difference between initializing and
clearing a structure like this:

mystruct_t a = {0};
and initializing and clearing it like this:

mystruct_t a;

memset(a, 0, sizeof(mystruct_t));

TIA,
Roubles


The first is an initialization the compiler does for you and the
second is one you do yourself. You know that. What is the real
question? As an aside, I note that the prototype..

void *memset(void *buffer, int ch, size_t num);

...suggests to me..

memset(&a, 0, sizeof a);

...but of course, it's just style, right?
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #4

P: n/a
Roubles wrote:
Hi All,

Quick question; what is the difference between initializing and
clearing a structure like this:

mystruct_t a = {0};
and initializing and clearing it like this:

mystruct_t a;

memset(a, 0, sizeof(mystruct_t));

TIA,
Roubles


Note that a is a structure, not a pointer. You need &a to take its
address for memset().
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.