CBFalconer wrote:
Michael wrote:
this seems to be similar to a
question I posted earlier. But
won't
*destination = *source;
just copy the adresses of the array,
s.th like a non-deep copy? So both
structs share the strings?
You removed context quotations, including all definitions. IIR
you defined the fields within the structures as arrays, not as
pointers, so there is nothing deep to copy.
The op definitions are a little unclear. There is some evidence
that the members are typedef char array of 20 characters. If this
is the case, struct assignment will be safe as will using function
strcpy.
Example:
#include <stdio.h>
#include <string.h>
typedef char variableName[20];
typedef struct info
{
variableName name;
variableName class;
variableName alignment;
}info;
info init = {"No data","No data", "No data"};
void AssignValues1(const info *source, info *destination)
{
*destination = *source;
}
void AssignValues2(const info *source, info *destination)
{
strcpy(destination->name,source->name);
strcpy(destination->class, source->class);
strcpy(destination->alignment,source->alignment);
return;
}
void PrintInfo(const info *p)
{
char *s = "No Data";
printf("Name: %s\nClass: %s\nAlignment: %s\n\n",
p->name,p->class,p->alignment);
}
int main(void)
{
info myinfo = {"George Washington","Political Science","Junior"};
info newinfo = init;
puts("struct newinfo contents");
PrintInfo(&newinfo);
newinfo = myinfo;
puts("Using simple assignment");
PrintInfo(&newinfo);
newinfo = init;
puts("Using function AssignValues1");
AssignValues1(&myinfo, &newinfo);
PrintInfo(&newinfo);
newinfo = init;
puts("Using function AssignValues2");
AssignValues2(&myinfo, &newinfo);
PrintInfo(&newinfo);
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto:
xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/