473,382 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Structure initialization and clearing

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
4 9515
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Skybuck Flying | last post by:
Hello, What does Const mean in this c structure ? and what is the delphi equivalent ? I think const struct just means it can't be modified... is that correct ? Struct { Type1 Field1;...
2
by: kimimaro | last post by:
hi I wonder if array can be work along with structure? Below are the declaration of my structure struct employee{ char ID; char Name; char Department;
1
by: int2str | last post by:
Hi! Inspired by a recent thread on this list, I'd like to ask the following: Given this code: struct S { int a;
5
by: aarklon | last post by:
Hi all, why the following structure initialization is not valid #include<stdio.h> struct rec { char name; int age; };
11
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
3
by: Martin | last post by:
Is clearing a structure the following way well defined in C89? The structure ACTION contains no floating point or pointer members. Only integral types. My thoughts concern the padding - can and...
17
by: jb.simon | last post by:
Recently I was pinged in a code review about my use of the initialization method AStruct myStruct = { 0 } ; Which initializes all elements of the myStruct to 0. I was questioned on it because...
14
by: Bill Reid | last post by:
OK, let's say that have a function menu structure declaration like this (since I basically do): typedef struct function_item { char *descrip; void(*function)(void); } t_function_item; ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.