0

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!

newrev426
  • 31
  • 3

1 Answers1

0

Not sure which syntax yo confused this with, but what you want is simply:

master.ptr = (struct var_alias_t*)malloc(sizeof(struct var_alias_t));

Also, you need to make sure you do your NULL checks before your access any members inside the thing you just malloc()ed. You never checked master.ptr->alias or master.ptr->command for NULL at all, and you accessed those values (which belong to master.ptr) before you checked to see if master.ptr itself was NULL.

SoapBox
  • 20,457
  • 3
  • 51
  • 87
  • Casting the result of `malloc` is considered bad style (http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – ChrisWue Sep 29 '13 at 20:09
  • Thanks for getting me out of my rut! I realized after my post I also am passing by value so I changed it to pass by reference and bam everything works perfectly! I will definitely add the error checking. Much appreciated. – newrev426 Sep 29 '13 at 21:27