Type mismatch warning

This is a discussion on Type mismatch warning within the C Programming forums, part of the General Programming Boards category; The following is my code that is supposed to act as a replacement shell for Linux: Code: #include "g5shell.h" /* ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    Type mismatch warning

    The following is my code that is supposed to act as a replacement shell for Linux:

    Code:
    #include "g5shell.h"
    
    /* Program buffers and work pointers */
    static char inpbuf[MAXBUF], tokbuf[2*MAXBUF], *ptr = inpbuf, *tok = tokbuf;
    
    /* ---- USERIN: Print prompt and read a line ---- */
    int userin(char *p)
    {
        int c, count;
        ptr = inpbuf;
        tok = tokbuf;
        
        printf("%s", p);                                        // Display prompt
        
        count =0;
        
        while(1){
            if ((c = getchar()) == EOF) return(EOF);
            if (count < MAXBUF) inpbuf[count++] = c;
            if (c == '\n' && count < MAXBUF){
                inpbuf[count] = '\0';
                return count;}
            if (c == '\n'){                                     // Restart if line too long
                printf("G5-Shell Error: Input line too long.\n");
                count = 0;
                printf("%s", p);}
        }
    }
    
    /* ---- GETTOK: Get token and place into tokbuf ---- */
    int gettok(char **outptr)
    {
        int type;
        
        *outptr = tok;                                          // Set the outptr string to tok
        while(*ptr == ' ' || *ptr == '\t') ptr++;               // Strip white space from the buffer containing the tokens
        *tok++ = *ptr;                                          // Set token pointer to the 1st token in the buffer
        switch(*ptr++){                                         // Set the type variable depending
            case '\n':                                          // on the token in the buffer
                    type = EOL;
                    break;
            case '&':
                    type = AMPERSAND;
                    break;
            case ';':
                    type = SEMICOLON;
                    break;
            default:
                    type = ARG;
                    while(inarg(*ptr))                          // Keep reading valid ordinary chars
                        *tok++ = *ptr++;
        }
        
        *tok++ = '\0';
        return type;
    }
    
    /* List of special chars for inarg() */
    static char special [] = {' ','\t','&',';','\n','\0'};
    /* ---- INARG: Check for special chars ---- */
    int inarg(char c)
    {
        char *wrk;
        
        for(wrk=special; *wrk; wrk++){
            if(c == *wrk) return(0);}
        return(1);
    }
    
    /* ---- PROCLINE: Process input line ---- */
    int procline(void)
    {
        char *arg[MAXARG+1];                                    // Pointer array for runcommand
        int toktype;                                            // Type of token in command
        int narg;                                               // Number of args so far
        int type;                                               // FOREGROUND or BACKGROUND
        
        narg = 0;
        
        for(;;){                                                // Infinite loop
            switch(toktype=gettok(&arg[narg])){                 // Take action according to token types
                case ARG:       if(narg<MAXARG) narg++;
                                break;
                case EOL:
                case SEMICOLON:
                case AMPERSAND: if(toktype == AMPERSAND) type = BACKGROUND;
                                else type = FOREGROUND;
                                if(narg != 0){
                                    arg[narg] = NULL;
                                    runcommand(arg, type);}
                                if(toktype == EOL) return;
    
                                narg = 0;
                                break;
            }
        }
    }
    
    /* ---- RUNCOMMAND: Execute command with optional wait ---- */
    int runcommand(char **cline, int where)
    {
        pid_t pid;
        int status;
        
        switch(pid = fork()){
            case -1:    perror("G5-Shell");
                        return(-1);
            case 0:     execvp(*cline, cline);
                        perror(*cline);
                        exit(1);
        }
        
        if(where == BACKGROUND){                                // If BACKGROUND process, then print pid and exit
            printf("[Process id %d]\n", pid);
            return(0);}
        if(waitpid(pid, &status, 0) == -1) return;              // Wait until process pid exits
        else return(status);
    }
    
    /* Prompt */
    char *prompt = "Command> ";
    
    /* ---- MAIN: Main function ---- */
    main()
    {
        char cmd;
        
        execl("/bin/clear", "clear", (char *)0);                // clear screen
        printf("Welcome to Group 5 Shell System.\n");
        
        while(userin(prompt) != EOF) procline();
    }
    After trying to compile I get the following 2 errors (faulty lines are highlighted on the code):
    shell.c:62: warning: type mismatch with previous implicit declaration
    shell.c:50: warning: previous implicit declaration of `inarg'

    I can't figure out why this is happening. Any help is appreciated.

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    ptr isn't declared (or initialized) in gettok(), so the compiler has assumed it's an int. Try declaring it as a char * (and initializing it!) at the start of gettok() and the compiler warning should go away.

    <edit> Declared static global - nasty!!
    Last edited by SKeane; 10-11-2006 at 03:11 AM.

  3. #3
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    You need to declare the inarg (for example after static declaration
    Code:
    int inarg(char);
    ) func or define it before gettok func.

  4. #4
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    Quote Originally Posted by SKeane
    ptr isn't declared (or initialized) in gettok(), so the compiler has assumed it's an int. Try declaring it as a char * (and initializing it!) at the start of gettok() and the compiler warning should go away.
    Its declared as static.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You didn't prototype (or define) inarg before you've started using it.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    Thanks all. Problem solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21