C 给指针字符赋值?

C 给指针字符赋值?,c,struct,strncpy,C,Struct,Strncpy,} 如何为*name赋值。为结构分配内存后,我为名称分配内存 struct group { char *name; struct user *users; struct xct *xcts; struct group *next; }; int add_group(Group **group_list_ptr, const char *group_name) { printf("%p\n",group_list_ptr); *group_list_ptr = m

}

如何为*name赋值。为结构分配内存后,我为名称分配内存

struct group {
    char *name;
    struct user *users;
    struct xct *xcts;
    struct group *next;
};

int add_group(Group **group_list_ptr, const char *group_name) {
printf("%p\n",group_list_ptr);
*group_list_ptr = malloc(sizeof(struct group));

printf("%p\n",*group_list_ptr);
printf("%p\n",(*group_list_ptr)->name);
(*group_list_ptr)->name = malloc(sizeof(*group_name));
printf("%p\n",(*group_list_ptr)->name);
strncpy((*group_list_ptr)->(*name), "hello", strlen(*group_name));
//printf("%s\n",(*group_list_ptr)->name);
return 0;
我正在用“hello”测试它,但我想复制const char*group_name

我会犯错误

strncpy((*group_list_ptr)->(*name), "hello", strlen(*group_name));
您不想取消对name成员的引用,这是编译器错误

您也不能使用sizeof来获取字符串的长度。使用strlen()


对于strcpy(),最后一个参数是要复制的字符串的长度。确保它小于目标缓冲区

我的坏习惯((*组列表)->(*名称),“你好”,sizeof(*组名称));抱歉,输入错误,结构有一个char*名称;strncpy((*组列表)->(*名称),“你好”,strlen(*组名称));但是结构中的数据类型是char*name。(*group_list_ptr)->名称,那么为什么这样做?好的,但您仍在复制“hello”和strlen'ing*group_名称。这也应该被清除。group_list_ptr中的name参数是char*,这是您要传递给strncpy的。这就是为什么(*group_list_ptr)->名称起作用的原因。
lists.c:24:32: error: expected identifier before ‘(’ token
lists.c:24:32: error: too few arguments to function ‘strncpy’
strncpy((*group_list_ptr)->name, "hello", strlen("hello"));