0

我是 C 编程的新手,很想得到一些帮助。

我有这个结构:

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

我想创建一个函数,它获取指向 HOUSE 的指针并返回指向位于内存中不同位置的同一个 HOUSE 的新指针 - 目的是能够更改一个指针而不更改两者:我会尽量清楚:

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!


}

我读到我们可以使用:memcpy_s 但在这里如果我在结构中只有整数可能很容易,这里我有 char * ,所以意味着我还需要char * houseName单独复制?我能做些什么?如何复制具有多种类型的对象,例如char *?如果我有一个数组?我能做什么 ?

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

我怎样才能复制它?

十分感谢!

4

1 回答 1

2

您需要复制结构以及结构管理所有内存。像这样:

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;
}

如您所见,两次分配的错误处理已经变得非常麻烦。如果您有多个内部分配,保持理智的唯一方法是goto系统地使用 s 来创建适当的清理点。


举例说明最后一点:

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;
}
于 2012-08-05T12:33:06.583 回答