Excuse me for the perhaps silly question, but is the following
permitted:
struct mystruct {
int f1;
int f2;
char f3;
float f4;
};
struct mystruct s1, s2;
s1.f1 = 10;
s1.f2 = 15;
s1.f3 = 'a';
s1.f4 = 5.5;
s2 = s1; /* or, if working with (struct mystruct *), even *s2 = *s1
*/
gcc does not warn about anything, and furthermore it seems to work
(ie, all the fields in s2 are copied correctly from s1), but this kind
of operation seems pretty new to me. Do the standards talk about this?
Thanks 8 5443
subnet wrote: Excuse me for the perhaps silly question, but is the following permitted:
struct mystruct { int f1; int f2; char f3; float f4; };
struct mystruct s1, s2;
s1.f1 = 10; s1.f2 = 15; s1.f3 = 'a'; s1.f4 = 5.5;
s2 = s1; /* or, if working with (struct mystruct *), even *s2 = *s1 */
gcc does not warn about anything, and furthermore it seems to work (ie, all the fields in s2 are copied correctly from s1), but this kind of operation seems pretty new to me. Do the standards talk about this?
Yes. It's perfectly legal (and has been since the first ANSI/ISO C).
In the second case, *s2 = *s1, it's only legal if s2 points to allocated
space (either dynamically allocated or automatic).
HTH,
--ag
--
Artie Gold -- Austin, Texas
"If you don't think it matters, you're not paying attention."
On Tue, 12 Oct 2004, subnet wrote: Excuse me for the perhaps silly question, but is the following permitted:
struct mystruct s1, s2;
s2 = s1;
Yes, that's perfectly valid C code. Structs can also be passed by value
to functions
foo(s1);
and returned from functions
struct s foo(void) {
struct s rc = {0};
return rc;
}
void bar(void) {
struct s tmp;
tmp = foo();
}
All this was introduced in the 1989 Standard, AFAIK. It wasn't present in
a lot of pre-Standard ("K&R") compilers.
(Because it wasn't in K&R, after I learned C I spent several years
avoiding these constructs in the belief that they were "inefficien t" ---
what looked like one line of C source code was expanding to several (or
several dozen!) lines of assembly behind my back. But then I realized
that (pragmatically) the 'a=b;' idiom is certainly no worse than the
'a.x=b.x; a.y=b.y; a.z=b.z;' idiom; and (philosophicall y) it made no sense
to worry about the "expansion" of my code by the compiler if the
alternative was to expand the code myself in the C source!)
Conclusion: Struct assignment is legal. Use it unless you absolutely
must retain portability to pre-C89 implementations .
HTH,
-Arthur
In <2t************ *@uni-berlin.de> Artie Gold <ar*******@aust in.rr.com> writes: subnet wrote: Excuse me for the perhaps silly question, but is the following permitted:
struct mystruct { int f1; int f2; char f3; float f4; };
struct mystruct s1, s2;
s1.f1 = 10; s1.f2 = 15; s1.f3 = 'a'; s1.f4 = 5.5;
s2 = s1; /* or, if working with (struct mystruct *), even *s2 = *s1 */
gcc does not warn about anything, and furthermore it seems to work (ie, all the fields in s2 are copied correctly from s1), but this kind of operation seems pretty new to me. Do the standards talk about this?
Yes. It's perfectly legal (and has been since the first ANSI/ISO C).
As anticipated by K&R1, it was actually legal since soon after K&R1 went
to print, when Unix V7 was released (it came with a one page addenda to
K&R1 document). At the same time, C acquired enumerated types.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
"subnet" <su****@katamai l.com> wrote in message
news:9d******** *************** ***@posting.goo gle.com... Excuse me for the perhaps silly question, but is the following permitted:
struct mystruct { int f1; int f2; char f3; float f4; };
struct mystruct s1, s2;
s1.f1 = 10; s1.f2 = 15; s1.f3 = 'a'; s1.f4 = 5.5;
s2 = s1; /* or, if working with (struct mystruct *), even *s2 = *s1 */
gcc does not warn about anything, and furthermore it seems to work (ie, all the fields in s2 are copied correctly from s1), but this kind of operation seems pretty new to me. Do the standards talk about this?
Thanks
Any specific reason why you think your text book does not talk about this
one? FWIW try this now
#include <stdio.h> // For printf
int main(void)
{
struct myStruct
{
int myArray[256];
};
myStruct myS1, myS2;
for ( int i = 0; i < 256; ++i )
{
myS1.myArray[i] = i;
myS2.myArray[i] = i + 1;
}
myS1 = myS2;
for ( int i = 0; i < 256; ++i)
{
printf("%d", myS1.myArray[i] );
}
return 0;
}
In C when you assign a structure to another, it is nothing more that plain
bit by bit copy.
HTH
--
Imanpreet Singh Arora
Zmoc.Zliamg@Zte erpnami
Remove Z to mail
"Things may come to those who wait, but only the things left by those who
hustle."
Abraham Lincoln
On Tue, 12 Oct 2004 10:13:16 -0400 (EDT), "Arthur J. O'Dwyer"
<aj*@nospam.and rew.cmu.edu> wrote: struct mystruct s1, s2; s2 = s1;
Yes, that's perfectly valid C code. Structs can also be passed by value to functions
foo(s1);
and returned from functions
struct s foo(void) { struct s rc = {0}; return rc; }
void bar(void) { struct s tmp; tmp = foo(); }
All this was introduced in the 1989 Standard, AFAIK. It wasn't present in a lot of pre-Standard ("K&R") compilers.
There's however one "caution!"-sign to be attached to the returning of
structs. Some compilers will use a shared statically allocated memory
area to hold intermediate return objects for struct objects instead of
on the stack, contrary to what a programmers gut feeling would tell
when looking at the syntax and semantics. This means reentrancy is
thrown overboard. While this is a perfectly legal implementation and
covered by the standard, it is extremely annoying when one wants to
build multi-threading or interrupt functions - I ran into this
"feature" with a compiler for a 16 bit microcontroller which is
specifically targeted for multiple tasks on multiple interrupt levels
(the compiler documentation even goes into great detail to explain how
to build multi-tasked programs).
Mark
On Tue, 12 Oct 2004, Mark Piffer wrote: On Tue, 12 Oct 2004 10:13:16 -0400 (EDT), "Arthur J. O'Dwyer" wrote: and returned from functions
struct s foo(void) { struct s rc = {0}; return rc; }
[...] There's however one "caution!"-sign to be attached to the returning of structs. Some compilers will use a shared statically allocated memory area to hold intermediate return objects for struct objects instead of on the stack, contrary to what a programmers gut feeling would tell when looking at the syntax and semantics. This means reentrancy is thrown overboard.
Reentrancy in the sense of multi-threaded-ness, yes? I see
multi-threaded-ness being thrown overboard (in the case that one function
writes to the shared "return" buffer before another function has a chance
to read its own data back out of it).
Such an implementation is still safe in single-threaded environments,
though --- you can call a struct-returning function recursively, for
example, and it will still work.
In theory, such an implementation is not significantly different from
one in which all 'int' values are returned in the accumulator register.
It's just that (I presume) most multi-threaded environments somehow make
non-interfering copies of the registers for different threads, but don't
make the same copies of the in-memory "returned struct" buffer.
-Arthur,
single-threaded su****@katamail .com (subnet) wrote in message news:<9d******* *************** ****@posting.go ogle.com>... Excuse me for the perhaps silly question, but is the following permitted:
struct mystruct { int f1; int f2; char f3; float f4; };
struct mystruct s1, s2;
s1.f1 = 10; s1.f2 = 15; s1.f3 = 'a'; s1.f4 = 5.5;
s2 = s1; /* or, if working with (struct mystruct *), even *s2 = *s1 */
gcc does not warn about anything, and furthermore it seems to work (ie, all the fields in s2 are copied correctly from s1), but this kind of operation seems pretty new to me. Do the standards talk about this?
Perfectly legal as all the other posters have pointed out. The
one thing you should beware off is structs that contain a pointer:
struct person_t {
int age;
char *name;
};
struct person_t leo, lee;
char test_name[] = "LEO";
....
leo.age = 42;
leo.name = test_string;
lee = leo;
printf ("%s\n", leo.f2); /* prints "LEO" as you expect */
printf ("%s\n", lee.f2); /* prints "LEO" as you expect */
lee.name[2] = 'E'; /* change the name from LEO to LEE */
printf ("%s\n", leo.name); /* prints "LEE", even though you think
that it was not modified. */
printf ("%s\n", lee.name); /* prints "LEE" as you expect */
structs that contain a pointer must be assigned carefully, as you
*may* not want the struct being assigned to (on the right hand side
of the equals) pointing to the same piece of data.
hand
goose,
not my /real/ name :-) ru**@webmail.co .za (goose) wrote in message news:<ff******* *************** ****@posting.go ogle.com>... Perfectly legal as all the other posters have pointed out. The one thing you should beware off is structs that contain a pointer:
Of course: plain s1 = s2 is just a "shallow" copy; I'm aware that to
do a deep copy, if the struct contains pointers, the elements pointed
to by those pointers need to be copied as well, and so on recursively.
Thanks everybody for your answers. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David Rasmussen |
last post by:
If I have this
struct S
{
int arr;
};
and I do this:
S s1,s2;
|
by: Matteo Settenvini |
last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using
g++ 3.3.x.
I had been taught that an array isn't a lot different from a pointer
(in fact you can use the pointer arithmetics to "browse" it). So I
expected that when I run this program, I get both c1.A and c2.A
pointing to the same address, and changing c1.A means that also c2.A
changes too.
----- BEGIN example CODE -----------
|
by: Roy Hills |
last post by:
When I'm reading from or writing to a network socket, I want to use a
struct to represent the structured data, but must use an unsigned char
buffer for the call to sendto() or recvfrom().
I have two questions:
1. Is it generally safe to "overlay" the structure on the buffer,
e.g.:
unsigned char buffer;
|
by: Bleakcabal |
last post by:
Keep in mind my program is written in C ( not C++ ).
I have a function which takes two structs as parameters is supposed to
put the values of the source struct in the destination struct. Only
the first character is put into the destination struct for each field.
void AssignValues(info *source, info *destination)
{
*destination->name = *source->name;
|
by: Victor Bazarov |
last post by:
Below you will find some code I wrote to see if I could wrap array copying
(especially for multi-dimensional arrays) in a simple class and/or
function. It seems to work fine for one-dimensioned arrays as well as
two-dimensioned ones. I am sure three- or more-dimensioned array are just
as OK here.
My concern was that I couldn't use 'std::copy' to copy multi-dimensional
arrays. Perhaps in the future we'll see specialisations of...
| |
by: Bidule |
last post by:
Hi,
I'm trying to sort structs defined as follows:
struct combinationRec {
float score;
char* name;
};
The number of structs and the length of the "name" field are not known
|
by: Christian Christmann |
last post by:
Hi,
I was wondering how the =operator works for
struct.
When I for example define a struct as follows:
struct point {
int a;
char *c;
|
by: barcaroller |
last post by:
Is it legal to compare the contents of two multi-field variables (of the
same struct) using "==" and "!="?
struct
{
int a;
int b;
} x,y;
...
|
by: Marty |
last post by:
I am new to C# and to structs so this could be easy or just not
possible.
I have a struct defined called Branch
If I use Branch myBranch = new Branch(i); // everything works
If I use Branch (myBranch + x) = new Branch(i); // it doesn't
x is a loop iterator, i is an int for the constructor to define an
array.
What am I doing wrong here.
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |