[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

create an array of strings

Posted on 2005-04-28
4
Medium Priority
?
195 Views
Last Modified: 2010-04-15
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
     
0
Comment
Question by:v_wall78
  • 4
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13890442
You need to use the strcpy function to copy a string
0
 
LVL 30

Expert Comment

by:Axter
ID: 13890448
       if (count < HIS_MAX-1 && history[count] != 0)
          strcpy(line,history[count]);
0
 
LVL 30

Expert Comment

by:Axter
ID: 13890465
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
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 13890478
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses
Course of the Month19 days, 10 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question