I've created a linked list were each node contains a structure as element and a pointer to the next node as follows

**list.h**

```
typedef struct node {
        group data;
        struct node *next;
} node;

typedef struct group {                                  
        unsigned int elements_count;                    
        unsigned int closed;                            
        unsigned int members[4];                        
} group;

```

**list.c**

```
node *add(node *head, group toadd) {
    node *n_node = (node*) malloc(sizeof(node));
    if(n_node != NULL) {
        n_node->next = head;
        group *n_group = &n_node->data;
        /* Copy the values of the group into the created node */
        n_group->elements_count = toadd.elements_count;
        n_group->closed = toadd.closed;
        for(int i  = 0; i < 4; i++) 
            n_group->members[i] = toadd.members[i];
    } 
    else {
        throw_error("malloc returned a NULL pointer");
    }
    return n_node;
}

```

The problem arise when I try to read the first element of the array (`node->data.members[0]`).
Valgrind says that the problem is an invalid read of size 4 where the address is not stack'd, malloc'd or (recentrly) free'd.
Why am I getting a segmentation fault even if I have used malloc to allocate each node?

> 
> EDIT:
> 
> 
> 

**main.c**

```
node *group_list = NULL;

/* Other code here.. */

group *cur_group = is_present(group_list, msg_gest.mtype);

if(cur_group == NULL) {
    // The group isn't still present in the group list, then add it
    group new_group = {
        .elements_count = 0,
        .closed = 0,
        .members = {-1, -1 , -1, -1}
    };

    new_group.members[new_group.elements_count++] = msg_gest.mtype;
    new_group.members[new_group.elements_count++] = msg_gest.to_add;
    new_group.closed = msg_gest.to_close;
    group_list = add(group_list, new_group);
} else {
    cur_group->members[cur_group->elements_count++] = msg_gest.to_add;
    cur_group->closed = msg_gest.to_close;
}

```

**is\_present**

```
group* is_present(node *head, int matr) {

        group *c_group;
        node *c_node = head;

        while(c_node != NULL) {

                c_group = &c_node->data;

                if(*(c_group->members) == matr) // !!Segmentation fault is caused by this read
                         return c_group;
                printf("\n%d", *(c_group->members));

                c_node = c_node->next;
        }
        return NULL;
}

```