• C

create an array of strings

How would I go about creating an array of strings?
I have right now:

 char line[LINE_MAX];                                        // To store input line.
 char history[HIS_MAX][LINE_MAX];                            // History of commands.
  int  i =0, count, flag=0, his_count=0;

 if (line[i++] == '!') {                                 // If first character user entered was ! then use history
        count = 0;
        while (line[i++] != NULL) {
          count = count * 10;                                 // Find what index to use for history array
          count = count + ((int)argbuf[1] - 48);  }           //offset of ascii
        if (count < HIS_MAX-1 && history[count] != NULL)
          line = history[count];
        else {
          fprintf(stderr, "Syntax error in command.\n");
          flag = 1; } }
      else {
        if (his_count++ < HIS_MAX-1) {
          i=0;
          while (history[i] != NULL)
            i++;
          history[i] = line; }
        else {
          for (i=0; i < HIS_MAX-1; i++)
            history[i] = history[i+1];
          history[i+1] = line; } }

But when I try to copy line into history, I get an error: incompatible types in assignment
what am I doing wrong?
Thanks
     
v_wall78Asked:
Who is Participating?
 
AxterCommented:
Don't treat a string like you do an integer.

You can't make comparison with a string like you do an integer, and you can't copy it either.

You need to use str??? functions to copy, compare, and assign data to a string.
0
 
AxterCommented:
You need to use the strcpy function to copy a string
0
 
AxterCommented:
       if (count < HIS_MAX-1 && history[count] != 0)
          strcpy(line,history[count]);
0
 
AxterCommented:
Also, don't put a minus one on the if condition statement.
And you should be checking for the first char equal to zero or just using strlen(str) != 0
Example:
       if (count < HIS_MAX && history[count][0] != 0)
          strcpy(line,history[count]);

or ----

       if (count < HIS_MAX && strlen(history[count]) != 0)
          strcpy(line,history[count]);

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.