I have a struct containing char** and I need to assign its members in a function, but the following assignment doesn't work:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char **clist;
} rule;
void assign(rule *rule) {
char buf[128] = "hello";
rule->clist[0] = buf;
}
int main()
{
rule rule;
rule.clist = malloc(sizeof(char*) * 8);
assign(&rule);
printf("%s\n", rule.clist[0]);
return 0;
}
Assigning as follows in the main function works though:
char buf[] = "hello";
rule.clist[0] = buf;