I'm trying to copy a recieved char-array (cur) into cur2. Cur is a line read by fgets from user-input.
I need a copy because cur is later divided into words, but cur2 should hold the entire line. A simplified code-snippet:
The idea is that temp[] should hold each input-line, but somehow every element is replaced by the input .Code:#define MAX 20000 #define LINE 120 #define WORDS 20 void run (char cur[]) { char *param[MAX]; char *temp[MAX]; char cur2[WORDS]; int s; cur[strlen(cur)-1] = '\0'; strcpy(cur2, cur); temp[s] = cur2; s++; } int main (void) { char *cur; while (1) { printf("input >"); fflush(stdout); cur = malloc(LINE); if (fgets(cur,LINE,stdin) == NULL) { free(cur); break; } if (n>=MAX) { printf("buffer overflow..\n"); exit(0); } run(cur); } exit(0); }
If input is:
> pwd
> ls -la
> h
then temp[0], temp[1], and temp[2] now contains 'h'. Any help would be appreciated!!