C has no copy constructor. Constructors, destructors and special member functions belong to C++ classes, and C has no classes. In C you copy a struct with plain assignment, which copies every member byte for byte, or with a function you write yourself when the struct owns memory.
The distinction matters because C++ developers expect copying to be customisable. In C, copying is always a shallow, member-wise copy unless you take control with your own function.
A copy constructor is a C++ feature: a constructor of the form T(const T&) that the compiler calls automatically when an object is initialised from another object of the same type. C has no references, no overloading and no automatic function calls on initialisation, so nothing runs when you write struct S b = a;. The compiler simply copies the bytes.
This also means C has no destructors. You must release resources explicitly, and you must decide yourself who owns what.
Structs can be assigned and passed or returned by value. Each member is copied, including whole arrays declared inside the struct.
Because label is an array inside the struct, b has its own copy and changing it does not affect a. Assignment is the right tool for structs that contain only values and fixed-size arrays.
If a member is a pointer, assignment copies the pointer, not the data it points to. Both structs then refer to the same memory. Modifying it through one changes the other, and freeing it through both is a double free, which is undefined behaviour.
The C equivalent of a copy constructor is a function that allocates new memory and copies the contents. Return an error code so the caller can handle allocation failure, and write a matching function that frees the resources.
strdup is part of the standard from C23 and available on POSIX systems earlier, but writing dup_string keeps the example portable. Note that the destination is written only after every allocation has succeeded, so a failure leaves it untouched.
| Situation | Approach |
|---|---|
| Only values and fixed arrays | Assignment: b = a; |
| Owns heap memory | Deep-copy function plus a matching free function |
| Large data you do not need to modify | Share a pointer and document who owns it |
| Block of bytes | memcpy, only when source and destination do not overlap (use memmove otherwise) |
Do not use memcpy on a struct that contains pointers expecting a deep copy: it is the same shallow copy as assignment.
No. It is a C++ concept. In C you use struct assignment for simple data and write your own functions for deep copies.
Allocate new storage for each pointer member, copy the pointed-to data into it, and copy plain members by value. Provide a function that frees everything again.
memcpy instead of a copy function?Only for a shallow copy. memcpy copies pointer values, so both structs still share the same heap data.
The copy points back to the original, not to itself. Fix up such pointers manually after copying.
C has no copy constructor, so copying is either automatic and shallow (struct assignment) or explicit and deep (a function you write). Whenever a struct owns heap memory, pair a copy function with a free function and check every allocation.