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

  • Home
  • SEARCH
  • 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 8347607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:24:34+00:00 2026-06-09T07:24:34+00:00

I’m new in C programming and would love to get some help. I have

  • 0

I’m new in C programming and would love to get some help.

I have this struct :

typedef struct house
{
   int numOfRooms;
   char* houseName;
}HOUSE,*pHOUSE;

I want to create a function that gets a pointer to HOUSE and returns a new pointer to same HOUSE that is located in a different place in memory – the purpose is to be able to change one pointer without changing both :
I will try to be clearer :

pHOUSE duplicate_house(pHOUSE house)
{
  pHOUSE newh = (pHOUSE)calloc(1,sizeof(HOUSE));
  newh = house 
 //I get here a pointer that points on the same house.. so If I change for exmample:
 //  newh->numOfRooms = 9 - > both will change and I don't want it to happen!


}

I read that we can use : memcpy_s
but here If I only had integers inside the struct it could be easy , here I have char * , so means I need also to copy the char * houseName separately?
what can I do? How can I copy an object that has multiply types like char *?
and If I had an array ? what could I do ?

typedef struct house
{
    int numOfRooms;
     char* houseName;
     struct house *houses[10];
}HOUSE,*pHOUSE;

how can I copy that?

thank u very much!

  • 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-06-09T07:24:36+00:00Added an answer on June 9, 2026 at 7:24 am

    You need to copy both the structure as well as all memory that’s managed by the structure. Like so:

    pHOUSE copy(pHOUSE house)
    {
        pHOUSE newHouse = malloc(sizeof *newHouse);      // allocate
    
        if (pHOUSE)
        {
            memcpy(newHouse, house, sizeof *newHouse);   // or "*newHouse = *house;"
            size_t const len = strlen(house->houseName);
            newHouse->houseName = malloc(len + 1);
    
            if (!newHouse->houseName) { free newHouse; return NULL; }
    
            strncpy(newHouse->houseName, house->houseName, len + 1);
        }
        return pHOUSE;
    }
    

    As you can see, the error handling with two allocations is already getting very cumbersome. If you had multiple internal allocations, the only way to remain sane is to use gotos systematically to create the appropriate cleanup points.


    Example to illustrate the last point:

    struct FooLish
    {
        char * p1;
        char * p2;
        char * p3;
    };
    
    struct FooLish * copy(struct FooLish const * oldFoo)
    {
        struct FooLish * newFoo = malloc(sizeof *newFoo);
        if (!newFoo) { goto end0; }
    
        {
            size_t const len = strlen(oldFoo->p1);
            newFoo->p1 = malloc(strlen(len + 1);
            if (!newFoo->p1) { goto end1; }
            strncpy(newFoo->p1, oldFoo->p1, len + 1);
        }
        {
            size_t const len = strlen(oldFoo->p2);
            newFoo->p2 = malloc(strlen(len + 1);
            if (!newFoo->p2) { goto end2; }
            strncpy(newFoo->p2, oldFoo->p2, len + 1);
        }
        {
            size_t const len = strlen(oldFoo->p3);
            newFoo->p3 = malloc(strlen(len + 1);
            if (!newFoo->p3) { goto end3; }
            strncpy(newFoo->p3, oldFoo->p3, len + 1);
        }
    
        return newFoo;
    
    end3:
       free(newFoo->p2);
    end2:
       free(newFoo->p1);
    end1:
       free(newFoo);
    end0:
        return NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.