472,354 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

warning: comparison between pointer and integer

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
4 6077
horace1
1,510 Expert 1GB
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
thanks horace1...
y cant i use gets()? i change it to fgets() then it works.....
Jan 18 '07 #3
horace1
1,510 Expert 1GB
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
thanks man that really helps...
Jan 18 '07 #5

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

Similar topics

5
by: dis | last post by:
I've been going through my code and clearing away some of the compiler warnings that i'm generating and i've come across areas where i cast pointers to integer values. The Visual Studio compiler...
10
by: isxyos | last post by:
Hello, It's just a warning, but can anybody explain to me what this warning is: warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *' #include <stdio.h> void...
4
by: Dawn Minnis | last post by:
Hi When I compile my files I get the following: driver.c: In function `main': driver.c:49: warning: assignment makes integer from pointer without a cast driver.c:50: warning: assignment...
16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program...
29
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
7
by: llothar | last post by:
When i use -W4 on visual c 7.0 i get warning C4054 translator1.c(1703) : warning C4054: 'type cast' : from function pointer 'void * (__cdecl *)(se_agent *)' to data pointer 'void *'...
5
by: J.Broeden | last post by:
Hi, I've written a some code to assist in my understanding of strings using some of Cs built in character handling functions but I am not sure why I'm getting the following error. I hope someone...
20
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.