- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的 C 程序中有以下结构
struct person {
char key[50];
char color[20];
int age;
};
我想深拷贝这个结构。我已经设置了深度复制功能,但是我对如何深度复制字符串有点困惑。我听说有人使用 strcpy
和其他人使用 strdup
。
我在我的程序中想要的是如果原始人被释放,深度复制的人的键和颜色不受影响。一旦设置,键和颜色就不能改变。就我的目的而言,我应该使用 strcpy
还是 strdup
函数?
最佳答案
Post-K&R(即在标准 C 中)1 您可以直接分配它们。下面的函数只是为了让例子更清楚,你总是会就地分配:
void deepCopyPerson(struct person *target, struct person *src)
{
*target = *src;
}
详细说明:char 数组是结构对象的一部分(真正的数组,不仅仅是指针!),因此与对象一起分配和复制。
为了让不相信的人满意 ;-) 我在标准草案 1570 中翻找了一下:
6.5.16 Assignment operators
Semantics
An assignment operator stores a value in the object designated by the left operand. [Followed by type conversion and sequencing considerations which are not relevant here.]
[...]
6.5.16.1 Simple assignment
Constraints
One of the following shall hold:
- [...]
- the left operandhas an atomic, qualified, or unqualified version of a structure orunion type compatible with the type of the right;
[...]
Semantics
In simple assignment (=), the value of the right operand is convertedto the type of the assignment expression and replaces the value storedin the object designated by the left operand.
1 The first edition of The C Programming Language , 由贝尔实验室的 Brian W. Kernighan 和 Dennis M. Ritchie 编写并于 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.
特别是,您不能分配结构。 (但他们继续“这些限制将在即将发布的版本中删除”。)
事实上,这是 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.
他们阐述:
Copy and assignment include passing arguments to functions and returning values from functions as well.
这种等效性在今天的最新 C++ 标准中仍然适用。
因为我们在 C++ 的根管中:下一个语句是一个肯定的“结构可能无法比较。”有趣的是,他们添加了这句话 — 显然,内置comparison 在引入内置赋值后提出了自己的建议,但被拒绝了。在我看来,从来没有一个很好的理由:如果你可以按成员分配,为什么不以这种方式进行比较呢?在许多情况下这会很好,而且手动比较很难维护。用 spaceship operator 在 C++20 中使这成为可能又花了 30 年。它在(明确地)默认时生成所有其他比较;事实上,它默认为成员比较。
关于c - 在 C 中使用 char 数组深度复制结构(如何复制数组?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29989516/
我正在使用python 2.7 当我尝试在其上运行epsilon操作时出现此错误, 这是我的代码 import cv2 import numpy as np img = cv2.imread('img
1 很多程序员对互联网行业中广泛讨论的“35岁危机”表示不满,似乎所有的程序员都有着35岁的职业保质期。然而,随着AI技术的兴起,这场翻天覆地的技术革命正以更加残酷且直接的方式渗透到各行各业。程序员
我有一个包含多个子模块的项目,我想列出每个子模块的相对深度 该项目: main_project submodule1 submodule1\submodule1_1 submo
我有一张彩色图像及其深度图,它们都是由 Kinect 捕获的。我想将它投影到另一个位置(以查看它在另一个视角下的样子)。由于我没有 Kinect 的内在参数(相机参数);我该如何实现? P.S:我正在
给出了这三个网址: 1) https://example.com 2) https://example.com/app 3) https://example.com/app?param=hello 假
这个着色器(最后的代码)使用 raymarching 来渲染程序几何: 但是,在图像(上图)中,背景中的立方体应该部分遮挡粉红色实体;不是因为这个: struct fragmentOutput {
我希望能够在 ThreeJS 中创建一个房间。这是我到目前为止所拥有的: http://jsfiddle.net/7oyq4yqz/ var camera, scene, renderer, geom
我正在尝试通过编写小程序来学习 Haskell...所以我目前正在为简单表达式编写一个词法分析器/解析器。 (是的,我可以使用 Alex/Happy...但我想先学习核心语言)。 我的解析器本质上是一
我想使用像 [parse_ini_file][1] 这样的东西。 例如,我有一个 boot.ini 文件,我将加载该文件以进行进一步的处理: ;database connection sett
我正在使用 Mockito 来测试我的类(class)。我正在尝试使用深度 stub ,因为我没有办法在 Mockito 中的另一个模拟对象中注入(inject) Mock。 class MyServ
我试图在调整设备屏幕大小时重新排列布局,所以我这样做: if(screenOrientation == SCREEN_ORIENTATION_LANDSCAPE) { document
我正在 Ubuntu 上编写一个简单的 OpenGL 程序,它使用顶点数组绘制两个正方形(一个在另一个前面)。由于某种原因,GL_DEPTH_TEST 似乎不起作用。后面的物体出现在前面的物体前面
static FAST_FUNC int fileAction(const char *pathname, struct stat *sb UNUSED_PARAM, void *mo
我有这样的层次结构: namespace MyService{ class IBase { public: virtual ~IBase(){} protected: IPointer
我正在制作一个图片库,需要一些循环类别方面的帮助。下一个深度是图库配置文件中的已知设置,因此这不是关于无限深度循环的问题,而是循环已知深度并输出所有结果的最有效方法。 本质上,我想创建一个 包含系统中
如何以编程方式在树状结构上获取 n 深度迭代器?在根目录中我有 List 每个节点有 Map> n+1 深度。 我已修复 1 个深度: // DEPTH 1 nodeData.forEach(base
我正在构建一个包含大量自定义元素的 Polymer 单页界面。 现在我希望我的元素具有某种主样式,我可以在 index.html 或我的主要内容元素中定义它。可以这样想: index.html
我正在尝试每 25 秒连接到配对的蓝牙设备,通过 AlarmManager 安排,它会触发 WakefulBroadcastReceiver 以启动服务以进行连接。设备进入休眠状态后,前几个小时一切正
假设有一个有默认值的函数: int foo(int x=42); 如果这被其他人这样调用: int bar(int x=42) { return foo(x); } int moo(int x=42)
是否可以使用 Javascript 获取 url 深度(级别)? 如果我有这个网址:www.website.com/site/product/category/item -> depth=4www.w
我是一名优秀的程序员,十分优秀!