# Thread: compare pointer and integer?

1. actually, ms. laserlight, i became more confused of the terms you used. i'm only on my first year at learning c, and still at 3rd year high school.
i don't know what it means to change command_words from an array of 50 pointers to char, to an array of 50 arrays of char.

i just wanted to know how to seperate strings into the different words, variables or numbers (which serves as the commands) it has, as long as it is seperated by a space, because later on i will have to deal with strings with 4 commands such as:
Code:
`>>MMUL A B C`
which means that matrix A will be multiplied to matrix B and the answer will be stored to matrix C or
Code:
`>>SMUL 2 A B`
which means that a scalar(int), 2, will be multiplied to matrix A and the answer will be stored to matrix B or
Code:
`>>ADDR A 1 2 3`
which means that 1st row of matrix A will be added to its 2nd row and the answer will be stored to its 3rd row.

i really have no idea on how to do it, but i think i know the operations well. it's just that i have no idea how to deal with this kinds of strings. in fact, i only learned strtok while searching the net, and i'm not even sure if its the correct procedure. so please, teach me how to seperate the strings so i can use them in the if statements properly.

thanks for the help!

2. 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.

3. 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?

4. It simply means you cannot assign a pointer to an array.
An array is N elements successively from position 0, while a pointer simply points at some lone char. It doesn't make sense to be able to assign them.
strtok returns a pointer. The pointer points to the lone char at the start of a successive number of chars (an array).
I'm not sure what laserlight intended, but I'm sure laserlight can clarify further, if you wait a little.

5. i just wanted to know how to seperate strings into the different words, variables or numbers (which serves as the commands) it has, as long as it is seperated by a space, because later on i will have to deal with strings with 4 commands such as:
Code:
`>>MMUL A B C`
which means that matrix A will be multiplied to matrix B and the answer will be stored to matrix C or
Code:
`>>SMUL 2 A B`
which means that a scalar(int), 2, will be multiplied to matrix A and the answer will be stored to matrix B or
Code:
`>>ADDR A 1 2 3`
How about using "main(int argc, int * argv[])" to take in command line arguments?