Thread: compare pointer and integer?

  1. #16
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    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!
    Last edited by nyekknyakk; 08-19-2010 at 02:00 AM.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    Quote Originally Posted by Elysia View Post
    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. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  4. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  5. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM