When starting the shell function, the strcpy() call doesn't properly copy argdin to arg:
main.c:shell.c:Code:#include <stdio.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include "data.h" void randinit(void); int randint(int max); void create(struct ch * ma); void shell(struct ch * ma); int main(int argc, char * argv[]) { struct ch mai; int c; randinit(); if(!strcmp("--new", argv[1])) { create(&mai); } printf("Welcome, %s.",(&mai)->name); shell(&mai); printf("Bye!\n"); return 0; } void create(struct ch * ma) { char * name = malloc(64); printf("Name (64): "); fgets(name, 64, stdin); ma->name = malloc(strlen(name)); strncpy(ma->name, name, strlen(name) - 1); free(name); ma->total = 10; ma->life = ma->total; ma->pos[0] = 0; ma->pos[1] = 0; printf(STATS, ma->name, ma->life, ma->total, ma->pos[0], ma->pos[1]); }comm.c:Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #include "data.h" void shell(struct ch * ma) { char * argdin, * arg; int exit = 0, ret, time = 0; while(exit == 0) { argdin = malloc(256); printf("\nD: "); // printf("%s\n", stin); fgets(argdin, 255, stdin); printf("%s\n", argdin); arg = malloc(strlen(argdin) + 1); strncpy(arg, argdin, strlen(argdin) - 1); printf("%s\n", arg); // printf("arg = %s\n", arg); if(!strcmp("exit", arg) || !strcmp("quit", arg)) { exit = 1; } else if(!strncmp("move", arg, 4)) { ret = move(ma, arg); if(ret == 0) { printf("Move %c is out of bounds.", arg[5]); } else if(ret == 1) { printf("Moved %c.", arg[5]); } else if(ret == 2) { printf("Where is %c?", arg[5]); } } else if(!strcmp("stat", arg)) { printf(STATS, ma->name, ma->life, ma->total, ma->pos[0], ma->pos[1]); } else { if(time == 0) { time = 1; } else if(time == 1) { printf("Command not understood!"); } } free(argdin); } }randgen.c:Code:#include <stdio.h> #include <stdlib.h> #include "data.h" int move(struct ch * ma, char * com) { if(com[5] == 'u') { if(ma->pos[1] == 0) { return 0; } else { ma->pos[1] = ma->pos[1] - 1; return 1; } } else if(com[5] == 'd') { if(ma->pos[1] == YMAX) { return 0; } else { ma->pos[1] = ma->pos[1] + 1; return 1; } } else if(com[5] == 'l') { if(ma->pos[0] == 0) { return 0; } else { ma->pos[0] = ma->pos[0] - 1; return 1; } } else if(com[5] == 'r') { if(ma->pos[0] == XMAX) { return 0; } else { ma->pos[0] = ma->pos[0] + 1; return 1; } } else { return 2; } }data.h:Code:#include <stdio.h> #include <time.h> #include <stdlib.h> void randinit(void) { srand(time(NULL)); } int randint(int max) { return rand() % (max + 1); }Example Output:Code:struct ch { char * name; int total, life, pos[2]; }; #define STATS "%s\n%d/%d\nPOS[%d,%d]\n" // NAME, CURRENT LIFE, TOTAL LIFE, X POSITION, Y POSITION #define YMAX 256 #define XMAX 256Why does the first strcpy() not work correctly?Code:Name (64): Neil Neil 10/10 POS[0,0] Welcome, Neil. D: exit exit exitüƒ‼a D: exit exit exit Bye!