warning: comparison between pointer and integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • metalinc
    New Member
    • Jan 2007
    • 14

    #1

    warning: comparison between pointer and integer

    hi...im new to C programming...n eed help...tried to run this code but got this error

    fork.c: In function ‘parse’:
    fork.c:44: warning: comparison between pointer and integer
    fork.c:51: warning: assignment makes integer from pointer without a cast
    fork.c:61: warning: comparison between pointer and integer
    /tmp/cciECfg4.o: In function `main':
    fork.c:(.text+0 x3a): warning: the `gets' function is dangerous and should not be used.

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    main()
    {
        char buf[1024];
        char *args[64];
    
        for (;;) {
            /*
             * Prompt for and read a command.
             */
            printf("Command: ");
    
            if (gets(buf) == NULL) {
                printf("\n");
                exit(0);
            }
    
            /*
             * Split the string into arguments.
             */
            parse(buf, args);
    
            /*
             * Execute the command.
             */
            execute(args);
        }
    }
    
    /*
     * parse--split the command in buf into
     *         individual arguments.
     */
    parse(buf, args)
    char *buf;
    char **args;
    {
        while (*buf != NULL) {
            /*
             * Strip whitespace.  Use nulls, so
             * that the previous argument is terminated
             * automatically.
             */
            while ((*buf == ' ') || (*buf == '\t'))
                *buf++ = NULL;
    
            /*
             * Save the argument.
             */
            *args++ = buf;
    
            /*
             * Skip over the argument.
             */
            while ((*buf != NULL) && (*buf != ' ') && (*buf != '\t'))
                buf++;
        }
    
        *args = NULL;
    }
    
    /*
     * execute--spawn a child process and execute
     *           the program.
     */
    execute(args)
    char **args;
    {
        int pid, status;
    
        /*
         * Get a child process.
         */
        if ((pid = fork()) < 0) {
            perror("fork");
            exit(1);
    
    	/* NOTE: perror() produces a short  error  message  on  the  standard
               error describing the last error encountered during a call to
               a system or library function.
           */
        }
    
        /*
         * The child executes the code inside the if.
         */
        if (pid == 0) {
            execvp(*args, args);
            perror(*args);
            exit(1);
    
           /* NOTE: The execv() vnd execvp versions of execl() are useful when the
              number  of  arguments is unknown in advance;
              The arguments to execv() and execvp()  are the name
              of the file to be executed and a vector of strings  contain-
              ing  the  arguments.   The last argument string must be fol-
              lowed by a 0 pointer. 
    
              execlp() and execvp() are called with the same arguments  as
              execl()  and  execv(),  but duplicate the shell's actions in
              searching for an executable file in a list  of  directories.
              The directory list is obtained from the environment.
            */
        }
    
        /*
         * The parent executes the wait.
         */
        while (wait(&status) != pid)
            /* empty */ ;
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    rather than using NULL in statements such as
    Code:
    	while (*buf != NULL) {
    ....
    	*buf++ = NULL;
    should you be using the string terminator \0, e.g.
    Code:
    	while (*buf != '\0') {
    ....
    	*buf++ = '\0';

    Comment

    • metalinc
      New Member
      • Jan 2007
      • 14

      #3
      thanks horace1...
      y cant i use gets()? i change it to fgets() then it works.....

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by metalinc
        thanks horace1...
        y cant i use gets()? i change it to fgets() then it works.....
        I would have thought that either gets() or fgets() would work so long as you don't exceed the length of the destination array. fgets() is generally recommended because it will check for the array bounds being exceeded.

        The main differences between gets and fgets are:
        1 fgets stops reading after n - 1 characters (n is the length of the array)
        2 fgets includes the newline '\n' in the string (gets does not)
        3 fgets can read from any input stream, e.g. a file (gets reads from stdin only

        note that fgets() leaves \n in the array which usually has to be explicitly removed.

        Comment

        • metalinc
          New Member
          • Jan 2007
          • 14

          #5
          thanks man that really helps...

          Comment

          Working...