473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 6483
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.