c - Segmentation fault with strcpy() with array of pointers to structures -
constants:
#define max_opcode_name_len 4 i have array of structs:
opcode *mot[num_opcodes]; struct def:
typedef struct opcode { char name[max_opcode_name_len + 1]; char format; int type; } opcode; in code:
strcpy(mot[0]->name, "hlt"); strcpy(mot[1]->name, "add"); strcpy(mot[2]->name, "sub"); // seg fault on command strcpy(mot[3]->name, "mul"); // ...more code follows my code gives me segmentation fault here , i'm not sure why, since should have enough space hold 5 characters (4 char followed '\0'), shouldn't running out of space, , i'm copying string literal static memory location. perhaps defined struct incorrectly or used pointer arrow in wrong spot?
opcode *mot[num_opcodes]; is array of pointers opcode. not array of opcodes.
you have either allocate opcode memory each pointer stored in mot or (easier way current code) make mot array of opcodes
opcode mot[num_opcodes]; ^^ and access values as
strcpy(mot[0].name, "hlt"); strcpy(mot[1].name, "add"); .... ^^
Comments
Post a Comment