How to fill a structure when a pointer to it, is passed as an argument to a function
        Posted  
        
            by 
                Ram
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Ram
        
        
        
        Published on 2012-11-14T04:57:08Z
        Indexed on 
            2012/11/14
            4:59 UTC
        
        
        Read the original article
        Hit count: 180
        
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.
© Stack Overflow or respective owner