v_wall78
asked on
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
I have right now:
char line[LINE_MAX]; // To store input line.
char history[HIS_MAX][LINE_MAX]
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
You need to use the strcpy function to copy a string
if (count < HIS_MAX-1 && history[count] != 0)
strcpy(line,history[count] );
strcpy(line,history[count]
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] );
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]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.