通过引用传递结构并对其进行操作
typedef struct unit_class_struct {
char *name;
char *last_name;
} person;
int setName(person *array) {
array[0].name = strdup("Bob");
array[1].name = strdup("Dick");
return 1;
}
int setLastName(person *array) {
array->last_name = strdup("Sanchez");
array++;
array->last_name = strdup("Clark");
return 1;
}
int main()
{
person array[10];
person *pointer;
pointer = array;
setName(pointer);
setLastName(pointer);
printf("First name is %s %s\n", array[0].name, array[0].last_name);
printf("Second name is %s %s\n", array[1].name, array[1].last_name);
while(1) {}
return 0;
}
这是我想出的一些用于处理结构的示例代码。 请注意我在 setName 中设置名称的方式以及在 setLastName 中设置名称的方式。
两者都有效,但我很好奇我的两种方法有什么区别?
一种方法比另一种方法更好吗?
在这个例子中strdup也是必要的吗?如果不是,如果我将 array.name 设置为随机大小的变量而不是字符串文字,是否有必要?
typedef struct unit_class_struct {
char *name;
char *last_name;
} person;
int setName(person *array) {
array[0].name = strdup("Bob");
array[1].name = strdup("Dick");
return 1;
}
int setLastName(person *array) {
array->last_name = strdup("Sanchez");
array++;
array->last_name = strdup("Clark");
return 1;
}
int main()
{
person array[10];
person *pointer;
pointer = array;
setName(pointer);
setLastName(pointer);
printf("First name is %s %s\n", array[0].name, array[0].last_name);
printf("Second name is %s %s\n", array[1].name, array[1].last_name);
while(1) {}
return 0;
}
This is some example code I came up with to play around with structures.
Notice the way I set the name in setName and the way I did it in setLastName.
Both work, but I'm curious whats the difference between the two ways I did it?
Is one way better than the other?
Also is strdup necessary in this example? If not, would it be necessary if I was setting array.name to random sized variables rather than string literals?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您将所有 .name 设置为字符串常量,则不,严格不需要
strdup
。如果您稍后要修改该字符串,您将需要它。对指针和内存管理有更好的了解将使区别变得清晰。如果您确实使用了strdup
,请务必稍后释放
结果。If you set all your .name's to string constants, then no,
strdup
is not strictly needed. You'll want it if you're going to modify the string later. A better grounding in pointer's and memory management will make the distinction clear. If you do usestrdup
, be sure tofree
the result later.这两个选项都相当危险,因为您不知道数组的长度。因此,访问 array++ 或 array[1] 的结果可能会导致未定义的行为。
也许您可以尝试这种方法
,其中 amount 是姓氏数组的长度。
请注意,没有使用 strdup。该函数期望用户为姓氏和数组分配内存。然后,如果需要,用户应该释放该内存。
我不喜欢 strdup 因为它返回一个堆分配的字符串,对于您的函数,应该清楚地记录函数的用户必须释放它。
Both options are quite risky, because you don´t know the length of the array. Consequently, accessing the result of array++ or array[1] could result in undefined behaviour.
Perhaps you could try this apprach
where amount is the length of the lastnames array.
Note that theres no usage of strdup. This function would expect the user to allocate memory for lastnames and array. Then the user should free that memory , if needed.
I dislike strdup because it returns a heap allocated string, and in the case of your functions, it should be clearly documented that the user of the functions has to free it.
在我看来,使用索引方法比使用指针算术稍微清晰一些,但两者都有效。我怀疑编译器为每个实例生成的代码在这两种情况下都非常相似。
如果您正在使用变量,则在这种情况下,strdup 是必需的,因为您没有分配内存来保存
person
结构中的字符串值。如果您在调用任一 setter 时已经为这些字符串分配了空间,那么strcpy
(最好是strcpy_s
)就足够了。当您在示例中使用文字时,不需要
strdup
:您可以直接分配给char*
。Using the indexed approach is slightly clearer than using the pointer arithmetic, in my opinion but either works. I suspect the code the compiler generates for each is quite similar in both instances.
strdup
is necessary in this instance if you're working with variables because you do not allocate memory to save your string values in in theperson
struct. If you'd already allocated space for those strings when either of your setters was called,strcpy
(strcpy_s
, preferably) would be sufficient.When working with literals as you are in your example,
strdup
is not necessary: you can assign directly to thechar*
.array->name
与(*array).name
完全相同。在这两个函数中,“array”都是指向结构的指针,因此
*array
和array[0]
都是结构,您可以设置它们的成员。箭头符号只是一种常用的快捷方式。array->name
is exactly the same as(*array).name
.In both your functions "array" is a pointer to a structure, so both
*array
andarray[0]
are structures, and you can set their members. The arrow notation is just a often used shortcut.