473,320 Members | 2,180 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,320 software developers and data experts.

curiosity: structure assignments in C (and unions within structs)


Hello,

In C++ AFAIK structure assignment copies every data member element by
element. This rule also works with the gcc 3.2.2 C compiler and even
works when one of the members is a union as in the following example
but I would like to know whether this is a gcc extension or whether
it's actually part of the C99 standard. Did previous C standards
allow this?

#include <stdio.h>

struct Foo { int a; int b; union { int x; float y; } z; };

int main(void) {
struct Foo hello = { 1, 2 }; /* OK */
hello.z.y = 1.1; /* OK */
printf("%f\n", hello.z.y); /* OK */
struct Foo hell = hello; /* OK */
printf("%d\n", hell.a); /* OK */
printf("%d\n", hell.b); /* OK */
printf("%f\n", hell.z.y); /* OK */
return 0;
}

Thanks,

Neil

Nov 13 '05 #1
1 4320
On Tue, 16 Sep 2003 23:59:06 -0230, Neil Zanella <nz******@cs.mun.ca>
wrote in comp.lang.c:

Hello,

In C++ AFAIK structure assignment copies every data member element by
element. This rule also works with the gcc 3.2.2 C compiler and even
works when one of the members is a union as in the following example
but I would like to know whether this is a gcc extension or whether
it's actually part of the C99 standard. Did previous C standards
allow this?

#include <stdio.h>

struct Foo { int a; int b; union { int x; float y; } z; };

int main(void) {
struct Foo hello = { 1, 2 }; /* OK */
hello.z.y = 1.1; /* OK */
printf("%f\n", hello.z.y); /* OK */
struct Foo hell = hello; /* OK */
printf("%d\n", hell.a); /* OK */
printf("%d\n", hell.b); /* OK */
printf("%f\n", hell.z.y); /* OK */
return 0;
}

Thanks,

Neil


No version of the C standard specifies how structures or unions are
copied. The only guarantee is that after the copy, they will compare
equal by suitable member-by-menber comparisons. There is no guarantee
that they will be identical as determined by memcmp().

Or are you asking whether C has structure and union assignments? For
almost 10 years before there was a C++ standard.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #2

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

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
44
by: Kosio | last post by:
-Hello, I'm looking for a way to deconstruct a (32 bit) float value to four (8 bit) char values. I then need a way to take those four (8 bit) char values and construct a (32 bit) float value. ...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
10
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
28
by: kyle york | last post by:
Greetings, Why does the C standard require the members of a structure not be re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which means one cannot rely on the exact layout...
38
by: sam_cit | last post by:
Hi, I have the following Struct, Struct Sample { int i; char *p; };
8
by: Andrew Smallshaw | last post by:
I'm working on a data structure that began life as a skip list derivative, but has evolved to the point that it now only has a passing resemblance to them. Each node of this structure has a few...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.