Originally Posted by
Elysia
Divide the terms into tokens.
50 pointers to char -> 50 pointers, char. That is: char* example[50]
Array of 50 arrays of char -> Array, 50 arrays, char. That is: char Example[N][50]
Where N is a positive number.
you mean to change char* cmd_words[50] to char cmd_words[50][50]?
ms. laserlight told me not to use global variables so if i combine your suggestions it will look like this:
Code:
#include <stdio.h>
#include <string.h>
void make_mat(void);
char get_cmd(void);
int main(void)
{
char cmd_words[50][50];
while (1)
{
get_cmd();
if (strcmp(cmd_words[0], "MAKE") == 0)
make_mat();
else if (strcmp(cmd_words[0], "DISP") == 0)
printf("DISP");
else
printf("Invalid Command!");
}
}
/*Get Command Function*/
char get_cmd(void)
{
char cmd_str[50] = "\0";
char cmd_words[50][50];
int loop;
printf("\n>>");
gets(cmd_str);
cmd_words[0] = strtok(cmd_str, " ");
if (cmd_words[0] == NULL)
{
printf("Error.\n");
}
for (loop = 1; loop < 50; loop++)
cmd_words[loop] = strtok(NULL, " ");
return *cmd_words[0];
}
/*Make Function*/
void make_mat(void)
{
int i, j;
char cmd_words[50][50];
if (strcmp(cmd_words[1], "A") == 0)
{
int mat_A[50][50];
int r1, c1;
printf("Enter rows: ");
scanf("%d", &r1);
printf("Enter columns: ");
scanf("%d", &c1);
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
{
printf("A[%d][%d] = ", i, j);
scanf("%d", &mat_A[i][j]);
}
}
else if (strcmp(cmd_words[1], "B") == 0)
{
int mat_B[50][50];
int r2, c2;
printf("Enter rows: ");
scanf("%d", &r2);
printf("Enter columns: ");
scanf("%d", &c2);
for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
{
printf("A[%d][%d] = ", i, j);
scanf("%d", &mat_B[i][j]);
}
}
else if (strcmp(cmd_words[1], "C") == 0)
{
int mat_C[50][50];
int r3, c3;
printf("Enter rows: ");
scanf("%d", &r3);
printf("Enter columns: ");
scanf("%d", &c3);
for (i = 0; i < r3; i++)
for (j = 0; j < c3; j++)
{
printf("A[%d][%d] = ", i, j);
scanf("%d", &mat_C[i][j]);
}
}
}
but my compiler said that in function, char get_cmd(), lines 33 & 40 have incompatible types in assignment of `char *' to `char[50]'. what does that mean?