In my program, i'm using the atoi() function to extract an int from argv. When I use
#include <stdlib.h>
I get the following error:
cachesim.c:20: warning: passing argument 1 of ‘atoi’ from incompatible pointer type
However, if I do not include the stdlib.h I receive no error at all and my code functions properly. Why is this?
#include <stdlib.h>
#include <stdio.h>
main(int argc, int **argv){
if(argc == 0){ int blk = 32; int i; for (i = 1; i < argc; i++){ if (strcmp(argv[i], "-b") == 0){ if (i + 1 <= argc - 1) blk = atoi(argv[i+1]); } } } }
main
should be:main(int argc, char *argv[]){
-- and the warning is because atoi takeschar *
and notint *
... when you don't include the header, the compiler should at least be emitting warnings about undefined function atoi. – JohnHachesim.c:20: warning: passing argument ...
? This hints,that without proper prototypes found in theinclude
files, the program may compile and run: run correctly or maybe not. – chux - Reinstate Monica