1

How to copy typedef struct into another one?

If I have a typedef struct called books and I have a struct variable called books first. Now I declare books second How can I pass the content of first to second? Is there a function to do that or I can do that just with second = first ?

  • 1
    How is `books` defined? – dbush Jan 30 '19 at 14:34
  • 1
    `typedef struct` is not much different from a `struct`. Only case with `typedef`: 1) you don't need to type `struct` 2) you have a higher chance of confusing people. – Sourav Ghosh Jan 30 '19 at 14:35
  • `second.foo = first.foo;` –  Jan 30 '19 at 14:38
  • 3
    Have you tried doing `second = first`? For simple a `struct` it should work just fine – Chris Turner Jan 30 '19 at 14:42
  • @ChrisTurner what is a simple `struct`? Even a `struct` with pointers can be a simple `struct` yet the pointers then require a decision about shallow versus deep copy. – Richard Chambers Jan 30 '19 at 15:58
  • @RichardChambers I consider a simple `struct` as one that doesn't contain pointers because you only need to do a shallow copy on it – Chris Turner Jan 30 '19 at 16:22
  • @ChrisTurner you need to be more detailed in your original comment for it to be useful. There are cases where a shallow copy of a `struct` with pointers is perfectly fine and provides the desired outcomes. And just doing a simple assignment as you suggest contradicts your second sentence and your second comment regarding pointers and copy since the OP has not actually specified what is in the `struct`. – Richard Chambers Jan 30 '19 at 18:23
  • @RichardChambers The point of my original comment was to suggest that the OP actually do some research rather than offering an actual solution to their question...if I were providing an answer, I'd have done so. – Chris Turner Jan 31 '19 at 10:14

2 Answers2

2

If your structure does not contain any members that are pointers (or containing structs that do), then you can simply assign one to the other.

second = first;

If however your structs do contain pointers, then with a simple assignment you end up with two struct that contain pointers to the same memory, and changing one will affect the other. If that's not what you want then you need to do a deep copy.

For example:

struct book {
    char *name;
    int cost;   // in cents, so you don't have to deal with floating point issues
};

struct book first;
first.name = strdup("title1");
first.cost = 500;

struct book second;
second.name = strdup(first.name);
second.cost = first.cost;
dbush
  • 205,898
  • 23
  • 218
  • 273
1

If both first and second have the same type, then you can just do second = first; . It does not matter whether the type is an built-in or user-defined. C will copy the contents of first over to second. Just try it.

In general, variables in C are just data with a name and a type. If the types of 2 variables a and b match, you can assign one to the other: a = b;. What happens is that the value of variable b is copied into variable a.

But beware of pointers: For C, pointers are just variables with a value (the fact that the value represents a memory address does not matter, C treats all data equal). If the 2 variables happen to be pointers, like char *a; char *b; then you can assign a = b; just with any variable. But since the value of the variable b is the memory address, the memory address is copied from b to a, not the content of the memory at the memory address.

If you want to have the memory copied over, you will have to do it on your own, e.g. via the help of memcpy() (see its man page for information).

That said, if your structs contain pointers, the pointers are the content, not the stuff the pointers point to. C would copy the pointer values, not the pointer targets. If you got pointers in your structs and want some sort of deep-copy, you would have to implement the traversal of your structs on your own. See What is the difference between a deep copy and a shallow copy?

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
Michael Beer
  • 853
  • 5
  • 12