DEV Community

Naman Tamrakar
Naman Tamrakar

Posted on

Ways to copy struct in c/c++

Today I am gonna show some of the ways to copy struct in c/c++.

Approach 1

In this approach we will copy attribute by attribute of struct into another struct to make a copy. The rest code is self explainable.

Code

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

typedef struct {
    char name[63];
    enum { MALE, FEMALE, OTHER } gender;
    int age;
    float height;
} User;

void print_user(User *user) {
    printf(
        "(Name: %s, Gender: %s, age: %d, height %.1f)\n",
        user->name,
        user->gender == MALE ? "Male" :
            user->gender == FEMALE ? "Female" : "Other",
        user->age,
        user->height
    );
}

int main() {
    User user = {"Naman", MALE, 21, 5}, user1;

    // Approach 1 
    strncpy(user1.name, user.name, sizeof(user.name));
    //        ^dest      ^src           ^size to copy 
    user1.gender = user.gender;
    user1.age = user.age;
    user1.height = user.height;

    printf("user: "); print_user(&user);
    printf("user copy: "); print_user(&user1);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output

user: (Name: Naman, Gender: Male, age: 21, height 5.0)
user copy: (Name: Naman, Gender: Male, age: 21, height 5.0)
Enter fullscreen mode Exit fullscreen mode

Disadvantage in this approach

Although It is very clear that this code is written for copying the struct. But as the size of struct increase It becomes more difficult to type all the attributes and So the chance of typo also increases.

So what's the solution for this. Let's discuss our next approach.


Approach 2

Instead of copying attribute by attributes We can also copy whole struct using memcpy and rest code will be same. Just copy whole struct in one line only.

// Inherit above code here

int main() {
    User user = {"Naman", MALE, 21, 5}, user1;

    // Approach 2
    memcpy(&user1, &user, sizeof(user));
    //      ^dest   ^src    ^size to copy 

    printf("user: "); print_user(&user);
    printf("user copy: "); print_user(&user1);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output

user: (Name: Naman, Gender: Male, age: 21, height 5.0)
user copy: (Name: Naman, Gender: Male, age: 21, height 5.0)
Enter fullscreen mode Exit fullscreen mode

Approach 3

We can also use assignment operator to make copy of struct.

Code

int main() {
    User user = {"Naman", MALE, 21, 5}, user1;

    // Approach 3
    user1 = user;

    printf("user: "); print_user(&user);
    printf("user copy: "); print_user(&user1);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output

user: (Name: Naman, Gender: Male, age: 21, height 5.0)
user copy: (Name: Naman, Gender: Male, age: 21, height 5.0)
Enter fullscreen mode Exit fullscreen mode

A lot of people don't even realize that they can copy a struct this way because one can't do same it with an array. Similarly one can return struct from a function and assign it but not array. The compiler do all the copying stuff behind the scene for us.

NOTE: When dealing with struct pointers approach 3 doesn't work as assigning one pointer to another doesn't make copy instead just point to new memory address. So if we modify new variable, original one is also affected.


❤️Thank you so much for reading it completely. If you find any mistake please let me know in comments.

Also consider sharing and giving a thumbs up If this post help you in any way.

Top comments (0)