www.hyebun.com <bkna@besta.cc> wrote:
You should only copy as many chars as are in the source string, otherwise
you access memory you don't own (BTW, sizeof(char) is 1 by definition).
What's wrong with a
strcpy( B.a.name, "Dr. Computer Cybernetics" );
or, if you're worried that the source string might be too long
strncpy( B.a.name, "Dr. Computer Cybernetics", 127 );
B.a.name[ 127 ] = '\0';
No, this won't do. Your structures are totally different, so you can't
use memcpy(). Not even considering issues of padding between structure
members, you would copy a pointer to an array (that's what's in B.A)
into an array (that's what you have in Ba.AA), which wouldn't give you
the results you expect. You need to copy all the elements of the
structures member by member, i.e.
strcpy( Ba.AA.name, B.A.name );
strcpy( BA.AA.addr, B.A.addr );
That's the only way to copy between different types of structures.
And if they would be identical you wouldn't need a memcpy(), a simple
assignment would do (and probably be safer).
--
_ _____ _____
| ||_ _||_ _| Jens.Toerring@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring