I'm trying to populate a linked list of structs from a global variable source and I get a BAD_ACCESS at the strcpy() line. Using C. Wondering if anyone could point out the issue.
The global struct is declared as so:
#include "data_structs.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_LEN 256
#define NUL '\0'
table_entry_t reg_list[]=
{
{"R0",0},{"PC",0},{"R1",1},{"SP",1},{"R2",2},{"SR",2},{"R3",3},
{"R4",4},{"R5",5},{"R6",6},{"R7",7},{"R8",8},{"R9",9},{"R10",10},
{"R11",11},{"R12",12},{"R13",13},{"R14",14},{"R15",15},{"R16",16}
};
... The structs looks like so (below) and are defined in a .h file.
typedef struct
{
char label[20];
int address;
}table_entry_t;
typedef struct
{
table_entry_t *data;
void *next;
} List_node_t;
typedef struct
{
List_node_t *head;
}list_t;
The linked list is initialized using: (below). The EXC_BAD_ACCESS occurs at the line "strcpy(new_node->data->label,reg_list[i].label);"
boolean List_init (list_t *list)
{
int all_ok = False;
int i=0;
char* temp[3];
if (list != NULL) {
list->head = NULL;
//Add Register Labels
while(i<20) // 20 register labels
{
List_node_t *new_node = NULL;
new_node = (List_node_t *) malloc( sizeof( List_node_t));
strcpy(new_node->data->label,reg_list[i].label); <---BAD ACCESS
new_node->data->address = reg_list[i].address;
new_node->next = list->head->next;
list->head->next = new_node;
}
all_ok = True;
}
return all_ok;
}
Appreciate the fresh set(s) of eyes. Regards.