How to fill a structure when a pointer to it, is passed as an argument to a function
- by Ram
I have a function:
func (struct passwd* pw)
{
struct passwd* temp;
struct passwd* save;
temp = getpwnam("someuser");
/* since getpwnam returns a pointer to a static
* data buffer, I am copying the returned struct
* to a local struct.
*/
if(temp) {
save = malloc(sizeof *save);
if (save) {
memcpy(save, temp, sizeof(struct passwd));
/* Here, I have to update passed pw* with this save struct. */
*pw = *save; /* (~ memcpy) */
}
}
}
The function which calls func(pw) is able to get the updated information.
But is it fine to use it as above.
The statement *pw = *save is not a deep copy.
I do not want to copy each and every member of structure one by one like
pw-pw_shell = strdup(save-pw_shell) etc.
Is there any better way to do it?
Thanks.