English 中文(简体)
C(如何复制阵列?)
原标题:Deep copying structs with char arrays in C (How to copy the arrays?)

我的C方案有以下内容:

struct person {
    char key[50];
    char color[20];
    int age;
};

I want to make a deep copy of this struct. I ve got my deep copy function setup however I m a bit confused about how to deep copy strings. I ve heard of people using strcpy and others using strdup.

我在方案中想要的是,如果原人获释,那么深重的被 co人的关键和肤色不会受到影响。 一旦确定,关键和肤色就不能改变。 为了我的目的,我是否应当使用<代码>strcpy或strdup功能?

最佳回答

<代码>strcpy()和strdup(>之间存在差异。

  • <>代码>strdup(> 分配空间,并将一个点贴在显示的复印件上,也得free(>. 返回点。

  • <代码>strcpy(> 系指所分配的空间和印本。

看来,在您的情形下,该代码为strcpy(),因为贵方结构的领域并非指点,因此,你可以向它们指定一个点子,用于分配空间,即strdup(>>。

However, as explained in this answer you actually don t need to.

问题回答

页: 1 下面的职能只是明确例子,你总是在内部分配:

void deepCopyPerson(struct person *target, struct person *src)
{
    *target = *src;
}

详细阐述: 这些char阵列是你们的 object物的一部分(真实阵列,不仅指点人!),因此被分配和复制。

In order to satisfy the disbeliefers ;-) I dug around in the standard draft 1570 :

<>6.5.16> 转让经营人<>/strong>

<>Semantics

转让经营人储存左轮经营所指定物体的价值。 [根据此处不适用的类型转换和排序考虑。]

[......]

<>0>6.5.16。 简单派任:

<>限制>

如下文之一应持有:

  • [......]
  • the left operand has an atomic, qualified, or unqualified version of a structure or union type compatible with the type of the right;

[......]

<>Semantics

In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.


1https://archive.org/details/TheCProgramingLanguage FirstEdition”rel=“nofollow noreferer”> 布赖恩·克里尼汉和丹尼斯·里奇在贝尔实验室撰写并于1978年出版的《方案拟订语言<>第一版》(;)是第一份C版的非正式语言说明,经常称为“K&R C”,在两本书作者注明该版本(以及10年之后具体指明的对ANSI C的区别)。 该书第一版载有第121页的以下句子:

The essential rules are that the only operations that you can perform on a structure are take its address with &, and access one of its members.

In particular, you cannot assign structures. (But they continue "these restrictions will be removed in forth- coming versions".)

事实上,这是10年之后的ANSI版本中语言的少数实质性(而不是偶然)增补内容之一。 Kernighan和Ritchie编写了一本书的“ANSI C版”。 第129页的这一句现在案文如下:

The only legal operations on a structure are copying and assigning to it as a unit, taking its address with &, and accessing its members.

它们详细阐述:

制版和派任还包括向职能提出论据和从职能中恢复价值。

如今,在最新的C++标准中,这种等同仍属实。

既然我们在这里是C++根基: 下一次发言是一份起诉书。 结构不可比较 令人感兴趣的是,他们添加了这一句——显然,在引入内在派任后,就提出自己的意见,但遭到拒绝。 我认为,从来就没有理由: 如果你能够分配成员,为什么也没有比较这种方式? 在许多情况下,这样做只是罚款,而人工比较难以维持。 在C++20和航天运营商,在(表面上)出现故障时产生所有其他比较结果;实际上,它没有进行成员-货物比较。

深度复制含有阵列的构件(无任何点人),深度拷贝简明。

struct person x = {"Key", "Color", 42};   /*  initialise to something */
struct person y = x;

如果“指示”是指点人的话,那就没有工作。 因此,有必要分配新的座标,然后使用像斯特鲁()这样的功能复制成员。

#include <stdlib.h>
#include <string.h>

struct pointer_person
{
    char *key;
    char *color;
    int age;
};

struct pointer_person deep_copy(struct pointer_person p)
{
     struct pointer_person retval;
     retval.key = malloc(strlen(p.key) + 1);
     strcpy(retval.key, p.key);
     retval.color = malloc(strlen(p.color) + 1);
     strcpy(retval.color, p.color);
     retval.age = p.age;
     return retval;
}

int main()
{
   struct pointer_person p;
   struct pointer_person pc;

   p.key = malloc(50);
   strcpy(p.key, "A key");
   p.color = malloc(20);
   strcpy(p.color, "A colour");
   p.key = 42;

   pc = deep_copy(p);

   /* do stuff with pc and c */

   free(p.key);
   free(p.color);
   free(pc.key);
   free(pc.color);
   return 0;
}

上面留下的一些错误核对(例如,需要核对malloc(>在复制前继承)。

如果你使用<代码>strcpy(>),你必须分配记忆。 www.un.org/Depts/DGACM/index_french.htm 您可以使用一种与原始编码分开的新记忆组,但自然而言,<代码>strdup(>更为简单,因为ttt需要一个单独的<条码>小孔(strlen()。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...