A
A
Alexander2016-01-21 22:08:18
C++ / C#
Alexander, 2016-01-21 22:08:18

Copying structures in C directly through the assignment operator, how correct is this?

To copy structures in C, I use the assignment operator (=), the compiler (gcc) does not give errors or warnings. Also I do return of structure from function.
Question: how correct is this? Where can you read about it?
Example:
struct sa { double d, int i };
struct ta { struct sa a, struct sa b, int c[12]};
typedef struct ta ta;
static ta ta1;
ta func(void) { return ta1 };
ta ta2 = func();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2016-01-21
@Ship63

It will work. The main thing to follow is that if there is a pointer in the structure, it is quite possible that deep copying is needed.
On the other hand, returning by value from a function, if the compiler doesn't know how to copy-elision, is expensive. That's why the standard library doesn't do that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question