Advertisement
STANAANDREY

clone deep copy

Apr 14th, 2025
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define NMAX 100
  6. typedef struct {
  7.   int id;
  8.   char name[NMAX];
  9. } Entity;
  10.  
  11. Entity cloneEntity(const Entity *const ep) {
  12.   Entity aux;
  13.   aux.id = ep->id;
  14.   strncpy(aux.name, ep->name, NMAX);
  15.   return aux;
  16. }
  17.  
  18. void printEntity(const Entity *const ep) {
  19.   printf("{ id: %d, name: %s }\n", ep->id, ep->name);
  20. }
  21.  
  22. int main(void) {
  23.   Entity e1 = {.id = 1, .name = "asd"}, e2;
  24.   e2 = cloneEntity(&e1);
  25.   strcat(e2.name, "123");
  26.   printEntity(&e1);
  27.   printEntity(&e2);
  28.   return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement