copy a struct

M

Michael

Hi all,

I want to copy a structure, but the only thing
I have is a pointer to it.

Whats good here? Because the structure contains no pointers,
but arrays and variables I thought of doing a memcpy:

#include <stdio.h>
#include <string.h>

struct test {
int a;
int b[2];
};

int main (void)
{
struct test t1, t2;
struct test *pt1;

pt1 = &t1;
t1.a = 1; t1.b[0] = 42; t1.b[1] = 173;

memcpy(&t2, pt1, sizeof(t2) );
printf ( "t1:a=%d, b_0=%d, b_1=%d\n", t1.a, t1.b[0], t1.b[1]);
printf ( "t2:a=%d, b_0=%d, b_1=%d\n", t2.a, t2.b[0], t2.b[1]);

return 0;
}
> gcc test.c -Wall
> a.out
t1:a=1, b_0=42, b_1=173
t2:a=1, b_0=42, b_1=173

It works, but I am a little confortable with this
solution. Would it be better to use a function, which
copy each variable?
> gcc test.c -Wall
> a.out
t1:a=1, b_0=42, b_1=173
t2:a=1, b_0=42, b_1=173


Thanks in advance
Michael
 
J

Jens.Toerring

Michael said:
I want to copy a structure, but the only thing
I have is a pointer to it.
Whats good here? Because the structure contains no pointers,
but arrays and variables I thought of doing a memcpy:
#include <stdio.h>
#include <string.h>
struct test {
int a;
int b[2];
};
int main (void)
{
struct test t1, t2;
struct test *pt1;
pt1 = &t1;
t1.a = 1; t1.b[0] = 42; t1.b[1] = 173;
memcpy(&t2, pt1, sizeof(t2) );
printf ( "t1:a=%d, b_0=%d, b_1=%d\n", t1.a, t1.b[0], t1.b[1]);
printf ( "t2:a=%d, b_0=%d, b_1=%d\n", t2.a, t2.b[0], t2.b[1]);
return 0;
}
t1:a=1, b_0=42, b_1=173
t2:a=1, b_0=42, b_1=173
It works, but I am a little confortable with this
solution. Would it be better to use a function, which
copy each variable?

You're looking for too complicated a solution. All you need is

t2 = *pt1;

and no memcpy() or other function calls. Note that structures are
"first class cititens" of C, you can copy them as a whole, pass
them to functions by value or return them from functions etc.

Regards, Jens
 
B

bjrnove

Hi.

memcpy is a good solution in this case since you wouldn't have to
change anything to add something else into the struct. Another thing
you can do is this:

t2 = *pt1;

Then it would be up to the compiler how to copy the items of the
struct.
 
G

Goran Larsson

Whats good here?

A simple assignment?

| #include <stdio.h>
| #include <string.h>
|
| struct test {
| int a;
| int b[2];
| };
|
| int main (void)
| {
| struct test t1, t2;
struct test t3;
| struct test *pt1;
|
| pt1 = &t1;
| t1.a = 1; t1.b[0] = 42; t1.b[1] = 173;
|
| memcpy(&t2, pt1, sizeof(t2) );
t3 = *pt1;
| printf ( "t1:a=%d, b_0=%d, b_1=%d\n", t1.a, t1.b[0], t1.b[1]);
| printf ( "t2:a=%d, b_0=%d, b_1=%d\n", t2.a, t2.b[0], t2.b[1]);
printf ( "t3:a=%d, b_0=%d, b_1=%d\n", t3.a, t3.b[0], t3.b[1]);
|
| return 0;
| }
| > gcc test.c -Wall
| > a.out
| t1:a=1, b_0=42, b_1=173
| t2:a=1, b_0=42, b_1=173
t3:a=1, b_0=42, b_1=173
It works, but I am a little confortable with this
solution. Would it be better to use a function, which
copy each variable?

Why are you trying to make it more complicated than it is?
 
P

pete

Michael said:
I want to copy a structure, but the only thing
I have is a pointer to it.
Whats good here? Because the structure contains no pointers,
but arrays and variables I thought of doing a memcpy:
#include <stdio.h>
#include <string.h>
struct test {
int a;
int b[2];
};
int main (void)
{
struct test t1, t2;
struct test *pt1;
pt1 = &t1;
t1.a = 1; t1.b[0] = 42; t1.b[1] = 173;
memcpy(&t2, pt1, sizeof(t2) );
printf ( "t1:a=%d, b_0=%d, b_1=%d\n", t1.a, t1.b[0], t1.b[1]);
printf ( "t2:a=%d, b_0=%d, b_1=%d\n", t2.a, t2.b[0], t2.b[1]);
return 0;
}
t1:a=1, b_0=42, b_1=173
t2:a=1, b_0=42, b_1=173
It works, but I am a little confortable with this
solution. Would it be better to use a function, which
copy each variable?

You're looking for too complicated a solution. All you need is

t2 = *pt1;

and no memcpy() or other function calls. Note that structures are
"first class cititens" of C, you can copy them as a whole, pass
them to functions by value or return them from functions etc.

I would put structures a step below first class.
You can't use any of the equality or relational operators
with structures.
 
M

Michael

Why are you trying to make it more complicated than it is?
I don't know, I get it thanks!

Thanks to all of you!
Michael
 
E

Emmanuel Delahaye

Michael wrote on 11/03/05 :
I want to copy a structure, but the only thing
I have is a pointer to it.

As long as the type is known and complete, you don't need more.


f (T *p)
{
T s = *p;
}

end of the story.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
M

Malcolm

You're looking for too complicated a solution. All you need is

t2 = *pt1;

and no memcpy() or other function calls. Note that structures are
"first class cititens" of C, you can copy them as a whole, pass
them to functions by value or return them from functions etc.
This is on my hit list of features that ought to be removed from the
language. It breaks the rule that no C statement may compile to more than a
few assembly instructions.
 
A

Alan Balmer

This is on my hit list of features that ought to be removed from the
language. It breaks the rule that no C statement may compile to more than a
few assembly instructions.
That rule would seem to be heavily dependent on the machine's
instruction set ;-)

On many machines, this would be loading a source pointer, a
destination pointer, a count, and one instruction.
 
M

Malcolm

Alan Balmer said:
That rule would seem to be heavily dependent on the machine's
instruction set ;-)
The other place the rule breaks down is where the compiler writer decides to
implement reasonable precision floating-point on a small processor with no
FPU. There's no easy way round this, since if you try to use functions
called things like flt_div() code rapidly becomes unreadable.

However you don't want a seemingly innocuous instruction like "x = y"
actually accounting for a narrow bottleneck in your code. When you gobble
lots of processor power, you wan't a warning.
On many machines, this would be loading a source pointer, a
destination pointer, a count, and one instruction.
What we are really interested in is the execution speed, not the number of
instructions.
It wouldn't be too difficult to build a massively parallel memcpy(), but I
don't think any are in common use. What you do have sometimes is the looping
logic implemented on chip, which speeds things up.

A lot of machines also have DMA engines, which can copy long structures in
parallel with main program flow. So you need to call DMcopy(), do a bit of
logic, and then retrieve your results. This is of course really awkward to
program, and only useful for huge structures.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top