473,795 Members | 3,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copying struct with array

If I have this

struct S
{
int arr[5];
};

and I do this:

S s1,s2;

....

s1 = s2;

what happens?

Normally, array assignments aren't possible/allowed. If I do s1.arr =
s2.arr I get a compiler error. If I do s1 = s2 it compiles fine. And it
even seems like the array is getting copied, not just the pointer to the
start of the array, at least with g++.

Is there any difference in semantics here betweeen C and C++?

/David
Jul 22 '05
10 11402
JKop wrote:
Now, take your example:

s1.ar = s2.ar;

That is equal to:

&s1.ar[0] = &s2.ar[0];

That's exactly like writing:

unsigned int k = 4;
&k = 27873;

You're not changing the value of a variable at all! You're trying to change
the address of a variable! Self-explanatory I hope!
...


This doesn't really explain much. Yes, the standard array-to-pointer
conversion is the immediate formal reason why such code won't compile,
but the _real_ question is why array assignment (and array copying)
contexts were not excluded from the list of contexts where
array-to-pointer conversion is applied. Today there is no formal reason
for it to be this way, although there probably was one a rather long
time ago.

There is no technical problems with requiring the language to perform
array assignment in

s1.ar = s2.ar;

But doing this will break the compatibility with previous version of
language specification.

Note also that one specific array-related context does actually copy arrays:

char lpsz[] = "Hello world";

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #11

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

Similar topics

5
17656
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;
12
9839
by: anonymous | last post by:
Hi folks, I am in a fix trying to copy data to an array which is member of a structure. What I am doing right now is: char array = {0,1,2,3,4,5,6,7}; memcpy(structure.array, array, 8); Is there a nicer way of doing this in a single statement?
5
3291
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...
3
2644
by: Giox | last post by:
Hello everybody I want to create a routine that mimic the fread behavior operating on char array. I wrote size_t fread_char(void* buf, size_t sz, size_t n, char* string_source) { memcpy(buf, string_source, sz*n); string_source += n*sz;
10
4249
by: webfan | last post by:
In the code below, will the assignment z= y do bitwise copying? struct x { int a; int b; int c; }; main()
1
2796
by: RonLandreth | last post by:
I am writing an accounting system for a class I'm taking at SLU. I need help figuring out the best way to go about copying a 2D array to a temporary 2D array, for eventually copying it back. Here's my flow of commands: 1. copy a single row of a 2D array 2. change that single row (either add a cell or delete a cell). 3. copy that single row back into the 2D array by the use of a temporary array. So I'm basically copying a single row...
9
4262
by: Mr John FO Evans | last post by:
Am looking at adding structures to an embedded C emulator but am wondering what the standard is for copying structures(ie structa=structb) which contain pointers to memory and which are therefore not contiguous in memory. When such structures are copied what should C do with these elements? Or should copying be illegal in this case? John
2
1401
by: DaTurk | last post by:
This is probably a very silly, and simple question. If I'm coding in CLI, and I want to copy an array to an array, not a deep copy, just something of the nature arr1 = arr2, what is going on? I assumed the address of the first element is getting copied, so i essentially have two handles to the same memory. And changes in one would be reflected in the other. But what if I have something like this
2
7208
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
Oralloy
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...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
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,...
1
7540
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
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();...
0
5437
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...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.