The below code is a example of my issue. I can search, insert, etc. into the Linked List but I only have 1 linked list. So I wanted to change all of my accessor functions to allow the passing of a struct that contains the members that will be worked on allowing for easy allocation of new Linked Lists of the var_alias_t type.
This code works great but want to modify to allow for this function and others to modify multiple lists of the var_alias_t type.
struct var_alias_t
{
char *alias;
char *command;
struct var_alias_t *next;
};
struct var_alias_t *ptr = NULL;
struct var_alias_t *head = NULL;
struct var_alias_t *curr = NULL;
struct var_alias_t* var_alias_ll_create_list(char *alias, char *cmd)
{
printf("\n creating list with headnode as [%s,%s]\n",alias,cmd);
struct var_alias_t *ptr = (struct var_alias_t*)malloc(sizeof(struct var_alias_t));
ptr->alias = malloc(strlen(alias)+1);
ptr->command = malloc(strlen(cmd)+1);
if(NULL == ptr)
{
printf("\n Node creation failed \n");
return NULL;
}
strcpy(ptr->alias,alias);
strcpy(ptr->command,cmd);
ptr->next = NULL;
head = curr = ptr;
return ptr;
}
My attempt at changing the code.
struct var_alias_t
{
char *alias;
char *command;
struct var_alias_t *next;
};
struct var_alias_t *ptr = NULL;
struct var_alias_t *head = NULL;
struct var_alias_t *curr = NULL;
typedef struct
{
struct var_alias_t *ptr;
struct var_alias_t *head;
struct var_alias_t *current;
}ll_tracking_t;
ll_tracking_t ll_var = {NULL,NULL,NULL};
ll_tracking_t ll_alias = {NULL,NULL,NULL};
struct var_alias_t* Xvar_alias_ll_create_list(char *part1, char *part2,ll_tracking_t master)
{
printf("\n creating list with headnode as [%s,%s]\n",part1,part2);
struct var_alias_t *(master.ptr) = (struct var_alias_t*)malloc(sizeof(struct var_alias_t));
master.ptr->alias = malloc(strlen(part1)+1);
master.ptr->command = malloc(strlen(part2)+1);
if(NULL == master.ptr)
{
printf("\n Node creation failed \n");
return NULL;
}
strcpy(master.ptr->alias,part1);
strcpy(master.ptr->command,part2);
//ptr->alias = alias;
//ptr->command = cmd;
master.ptr->next = NULL;
//strcpy(*Items,Data) ; // Here we copy it
master.head = master.current = master.ptr;
return master.ptr;
}
The error I get is on the below line is "error: expected ')' before '.' token"
struct var_alias_t *(master.ptr) = (struct var_alias_t*)malloc(sizeof(struct var_alias_t));
My understanding is I am guess wrong but the above statement means. Set the value of the master struct member named ptr which is a pointer to a malloced memory location the size of the var_alias_t struct with a pointer of the var_alias_t struct.
Thanks for any help I am new to all of this stuff!