通过引用传递结构并对其进行操作

发布于 2024-08-12 16:07:53 字数 889 浏览 9 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

好菇凉咱不稀罕他 2024-08-19 16:07:53

如果您将所有 .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 use strdup, be sure to free the result later.

归属感 2024-08-19 16:07:53

这两个选项都相当危险,因为您不知道数组的长度。因此,访问 array++ 或 array[1] 的结果可能会导致未定义的行为。

也许您可以尝试这种方法

int set_last_name(person* array, char* lastnames[],size_t amount){
    int i=0;

    for(;i<amount;i++,array++){
      strncpy(array->lastname,lastnames[i],strlen(lastnames[i]));
    }

     return 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

int set_last_name(person* array, char* lastnames[],size_t amount){
    int i=0;

    for(;i<amount;i++,array++){
      strncpy(array->lastname,lastnames[i],strlen(lastnames[i]));
    }

     return 1;
 }

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.

如果没有你 2024-08-19 16:07:53

在我看来,使用索引方法比使用指针算术稍微清晰一些,但两者都有效。我怀疑编译器为每个实例生成的代码在这两种情况下都非常相似。

如果您正在使用变量,则在这种情况下,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 the person 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 the char*.

心房敞 2024-08-19 16:07:53

array->name(*array).name 完全相同。

在这两个函数中,“array”都是指向结构的指针,因此 *arrayarray[0] 都是结构,您可以设置它们的成员。箭头符号只是一种常用的快捷方式。

array->name is exactly the same as (*array).name.

In both your functions "array" is a pointer to a structure, so both *array and array[0] are structures, and you can set their members. The arrow notation is just a often used shortcut.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文