473,513 Members | 3,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clearing a structure - syntax s = {0} question

Hi!

Inspired by a recent thread on this list, I'd like to ask the
following:

Given this code:

struct S
{
int a;
int b;
int c;
}

...

S s = {0};

How does this work? I mean it does clear the whole struct (at least
everywhere I tried), but what language feature causes this to happen?

Also, would this safely work for this struct S2?

struct S2
{
int a;

virtual void f();
}

Thanks,
Andre

Nov 23 '05 #1
1 1669

in*****@gmail.com wrote:
Hi!

Inspired by a recent thread on this list, I'd like to ask the
following:

Given this code:

struct S
{
int a;
int b;
int c;
}

...

S s = {0};

How does this work? I mean it does clear the whole struct (at least
everywhere I tried), but what language feature causes this to happen?
Structures like arrays and structs can be initialized by providing
partial data. The rest of the variable is filled with 0 (no matter
what arch or value of 0). "0" is guaranteed to be 0 for any type and
the initialization is recursive. The {0} above could be anything but
the first item would be initialized with whatever you provide. I don't
believe any other number would work for some types while the number 0
does.

Also, would this safely work for this struct S2?

struct S2
{
int a;

virtual void f();
}


It at least doesn't work over here. Having a virtual function creates
a "non-agrigate" type and this can't be initialized with {0}.

Nov 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3696
by: Martin Magnusson | last post by:
I'm having trouble clearing and resizing a static std::vector of std::vectors (segmentation fault). Is it OK to call clear() and resize() on a static attribute? My code is similar to the one posted below. The code below works, though. My original code crashes on the second call to X::f(). My question is: is this code valid, or is it just a...
14
1674
by: Jason | last post by:
Hi. I have a structure that was used in a previous iteration of a loop, and I want to clear out all the structures values that the previous iteration assigned without assigning every variable to be 0. I've thought of a few ways to do this, but they are all roundabout... Does anyone know of a better way to do this? Heres a sample idea...
4
9525
by: Roubles | last post by:
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:
15
2051
by: roberts.noah | last post by:
I ran across some code that called memset(this, 0, sizeof(*this)) in the member function of a structure in C++. This just looked wrong to me so I did some web searching and it does indeed seem like a questionable thing to do. Looking around further took me to a newsgroup discussion that said the better approach was to do something like sv =...
2
1356
by: eBob.com | last post by:
I know that this must be a really dumb question but I just can't find an answer. I want to associate some information with a RichTextBox. The Tag property seems to be the intended way to "hang" some additional information on a Control. I've created a structure, rtbAuxInfo, for the information. I think that I have found the right syntax...
13
2829
by: Adam Honek | last post by:
Instead of the ZeroMemory API what would one use in VB.NET to clean an array using a custom structure? The .clear isn't a member of it by default. Any suggestions? Thanks, Adam
65
8543
by: Leslie Kis-Adam | last post by:
Hi everyone! Does anyone know, if it is possible to clear the screen in ANSI C? If it is,then how? Any help would be appreciated. Laszlo Kis-Adam <dfighter_AT-NOSPAM_freemail.hu
17
3765
by: abhimanyu.v | last post by:
Hi Guys, I have one doubt. The test program is given below. It uses two way of finding out the offset of a variable in structure. I executed the program and found the same result. My question is what is difference between 1) (unsigned long) &((struct foobar *)0)->foo and
3
1761
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 should it be altered? typedef unsigned char Byte; #define MAX_ACTIONS 10 /* ... */
0
1889
by: Rafe | last post by:
Hi, This seems to be an old question, and I've read back a bit, but rather than assume the answer is "you can't do that", I'd thought I'd post my version of the question along with a reproducible error to illustrate my confusion. My problem is that I'm using Python inside XSI (a 3D graphics application). If I want to restart Python, I...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.