By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,823 Members | 730 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,823 IT Pros & Developers. It's quick & easy.

warning: comparison between pointer and integer

P: 14
hi...im new to C programming...need 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+0x3a): warning: the `gets' function is dangerous and should not be used.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4.  
  5. main()
  6. {
  7.     char buf[1024];
  8.     char *args[64];
  9.  
  10.     for (;;) {
  11.         /*
  12.          * Prompt for and read a command.
  13.          */
  14.         printf("Command: ");
  15.  
  16.         if (gets(buf) == NULL) {
  17.             printf("\n");
  18.             exit(0);
  19.         }
  20.  
  21.         /*
  22.          * Split the string into arguments.
  23.          */
  24.         parse(buf, args);
  25.  
  26.         /*
  27.          * Execute the command.
  28.          */
  29.         execute(args);
  30.     }
  31. }
  32.  
  33. /*
  34.  * parse--split the command in buf into
  35.  *         individual arguments.
  36.  */
  37. parse(buf, args)
  38. char *buf;
  39. char **args;
  40. {
  41.     while (*buf != NULL) {
  42.         /*
  43.          * Strip whitespace.  Use nulls, so
  44.          * that the previous argument is terminated
  45.          * automatically.
  46.          */
  47.         while ((*buf == ' ') || (*buf == '\t'))
  48.             *buf++ = NULL;
  49.  
  50.         /*
  51.          * Save the argument.
  52.          */
  53.         *args++ = buf;
  54.  
  55.         /*
  56.          * Skip over the argument.
  57.          */
  58.         while ((*buf != NULL) && (*buf != ' ') && (*buf != '\t'))
  59.             buf++;
  60.     }
  61.  
  62.     *args = NULL;
  63. }
  64.  
  65. /*
  66.  * execute--spawn a child process and execute
  67.  *           the program.
  68.  */
  69. execute(args)
  70. char **args;
  71. {
  72.     int pid, status;
  73.  
  74.     /*
  75.      * Get a child process.
  76.      */
  77.     if ((pid = fork()) < 0) {
  78.         perror("fork");
  79.         exit(1);
  80.  
  81.     /* NOTE: perror() produces a short  error  message  on  the  standard
  82.            error describing the last error encountered during a call to
  83.            a system or library function.
  84.        */
  85.     }
  86.  
  87.     /*
  88.      * The child executes the code inside the if.
  89.      */
  90.     if (pid == 0) {
  91.         execvp(*args, args);
  92.         perror(*args);
  93.         exit(1);
  94.  
  95.        /* NOTE: The execv() vnd execvp versions of execl() are useful when the
  96.           number  of  arguments is unknown in advance;
  97.           The arguments to execv() and execvp()  are the name
  98.           of the file to be executed and a vector of strings  contain-
  99.           ing  the  arguments.   The last argument string must be fol-
  100.           lowed by a 0 pointer. 
  101.  
  102.           execlp() and execvp() are called with the same arguments  as
  103.           execl()  and  execv(),  but duplicate the shell's actions in
  104.           searching for an executable file in a list  of  directories.
  105.           The directory list is obtained from the environment.
  106.         */
  107.     }
  108.  
  109.     /*
  110.      * The parent executes the wait.
  111.      */
  112.     while (wait(&status) != pid)
  113.         /* empty */ ;
  114. }
Jan 17 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 1,510
rather than using NULL in statements such as
Expand|Select|Wrap|Line Numbers
  1.     while (*buf != NULL) {
  2. ....
  3.     *buf++ = NULL;
  4.  
should you be using the string terminator \0, e.g.
Expand|Select|Wrap|Line Numbers
  1.     while (*buf != '\0') {
  2. ....
  3.     *buf++ = '\0';
  4.  
Jan 17 '07 #2

P: 14
thanks horace1...
y cant i use gets()? i change it to fgets() then it works.....
Jan 18 '07 #3

Expert 100+
P: 1,510
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.
Jan 18 '07 #4

P: 14
thanks man that really helps...
Jan 18 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.