c - Undefined elements in struct array -
c - Undefined elements in struct array -
i creating client/server application , want phone call functions dynamically. have create next struct:
typedef struct _cmd cmd; struct _cmd { const char *name; void (*func)(int s,int ac, char **av); };
when client send command server server browse through array of commands:
cmd cmds[] = { { "create", cmd_create }, { "exit" , cmd_exit }, { "list", cmd_list }, { "read", cmd_read }, { "delete", cmd_delete }, { "update", cmd_update } }; cmd *find_cmd(const char *name) { cmd *c; (c = cmds; c->name; c++) { if (stricmp(name, c->name) == 0) homecoming c; } homecoming null; }
please not
stricmp()
is not typo, case-insensitive version of strcmp.
i next problem. when phone call find_cmd() , pass invalid commmand, application crashes. debugging messages showed following:
browsing command: create browsing command: exit browsing command: list browsing command: read browsing command: delete browsing command: update browsing command: �p� browsing command: �(�
after segfault. looks me if there undefined elements in struct arraym come from? overlooking? in advance pointers.
you need "null" element @ end of list trigger c->name
(!= null
) test in loop.
change
cmd cmds[] = { { "create", cmd_create }, { "exit" , cmd_exit }, { "list", cmd_list }, { "read", cmd_read }, { "delete", cmd_delete }, { "update", cmd_update } };
to
cmd cmds[] = { { "create", cmd_create }, { "exit" , cmd_exit }, { "list", cmd_list }, { "read", cmd_read }, { "delete", cmd_delete }, { "update", cmd_update }, { null, null } };
c arrays struct segmentation-fault
Comments
Post a Comment