RandomGuy
RandomGuy

Reputation: 5229

assign value to pointer char?

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;

}

how can i assign a value to *name. After i allocated memory for the struct, I allocated memory for the name

strncpy((*group_list_ptr)->(*name), "hello", strlen(*group_name));

I am testing it out with "hello", but i want to copy const char *group_name.

I get errors

lists.c:24:32: error: expected identifier before ‘(’ token
lists.c:24:32: error: too few arguments to function ‘strncpy’

Upvotes: 0

Views: 2425

Answers (1)

PQuinn
PQuinn

Reputation: 1022

strncpy((*group_list_ptr)->name, "hello", strlen("hello"));

You don't want to dereference the name member, which is the compiler error.

You also can't use sizeof to get the length of a string. Use strlen().

For strcpy() the last parameter is the length of the string you're copying. Be sure it's smaller than the destination buffer!

Upvotes: 1

Related Questions