Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 937121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:25:30+00:00 2026-05-15T21:25:30+00:00

What is the standard way to copy two structs that contain char arrays? Here

  • 0

What is the standard way to copy two structs that contain char arrays?

Here is some code:

#include stdio.h>
#include string.h>
#include stdlib.h>

typedef struct {
    char* name;
    char* surname;
} person;

int main(void){
    person p1;
    person p2;

    p1.name     = (char*)malloc(5);
    p1.surname  = (char*)malloc(5);

    strcpy(p1.name, "AAAA");
    strcpy(p1.surname, "BBBB");

    memcpy(&p2, &p1, sizeof(person));
    free(p1.name);
    printf("%s\n", p2.name);
    return 0;
}

The line printf("%s\n", p2.name); does not print something, because I freed the buffer.

The problem with my structs is that they are bigger than struct person. They contain hundreds of char pointers, and I have to copy every member one by one.

Is there another way to copy two structs that contain char arrays without using malloc and strcpy for every member?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:25:31+00:00Added an answer on May 15, 2026 at 9:25 pm

    You have no choice but provide a copy function yourself:

    void copy_person(person *dst, const person *src)
    {
        dst->name = malloc(strlen(src->name) + 1);
        dst->surname = malloc(strlen(src->surname) + 1);
        strcpy(dst->name, src->name);
        strcpy(dst->surname, src->surname);
    }
    

    which may be more elaborated than that: checking for errors, factoring the strlen + strcpy in an auxilliary function, etc.

    That’s what copy constructors in C++ are for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a standard way / C# library to convert a string into a
Is there a standard way or library to copy and paste from a spreasheet
I built a report in Access that is too wide for the standard copy
Is there a standard way (i.e. defined by some Java/J2EE/etc. spec) to provide a
I have a lot of code in a big project that has two general
Im looking for fastest way to copy some tables from one sybase server (ase
The standard way of creating URLs in grails is: <a href=${createLink(controller:'news', action: 'show', params:
The standard way to share a Haskell library with other programmers is to create
is there a standard way to encode the user location (country, state/region, town), so
I just tryed the standard way for adding CToolbar to a dialog on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.