473,569 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

warning: comparison between pointer and integer

14 New Member
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.

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 6526
horace1
1,510 Recognized Expert Top Contributor
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
metalinc
14 New Member
thanks horace1...
y cant i use gets()? i change it to fgets() then it works.....
Jan 18 '07 #3
horace1
1,510 Recognized Expert Top Contributor
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
metalinc
14 New Member
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
11215
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 generates this warning ... "warning C4311: 'type cast' : pointer truncation from 'void *' to 'DWORD'" The warning is generated as the pointer is...
10
6159
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 swap(int *x, int *y); int main() { int x={1,6}, y={1,10}; printf("Before the function swap, x = %d and y = %d\n\n", x, y);
4
36168
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 makes integer from pointer without a cast driver.c:51: warning: assignment makes integer from pointer without a
16
5818
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 compiles fine. What is the warning shown ?
29
2488
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
3103
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 Function 'Dec2hms' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
7
5252
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 *' translator1.c(1703) : warning C4152: nonstandard extension, function/ data pointer conversion in expression whenever i cast a function pointer to a void* or...
5
11208
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 can explain the errors of my ways. /* Looking for special chars in testlist */ #include <stdio.h> #include <string.h> #include <ctype.h>
20
12386
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
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7665
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.