0
votes

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]);
                    }

            }
    } }
2
The declaration for main should be: main(int argc, char *argv[]){ -- and the warning is because atoi takes char * and not int *... when you don't include the header, the compiler should at least be emitting warnings about undefined function atoi.JohnH
Notice that "I get the following error:", is followed by a warning achesim.c:20: warning: passing argument ...? This hints,that without proper prototypes found in the include files, the program may compile and run: run correctly or maybe not.chux - Reinstate Monica

2 Answers

3
votes

Change:

main(int argc, int **argv){ 

to

int main(int argc, char *argv[]){

Your former declaration is not an acceptable declaration for main and atoi requires a parameter of type char *.

0
votes

argv is supposed to be a character array, you have to read the characters and then convert them into an int.

Also, what is:

if(argc == 0)

If you have no arguments? That should never even be possible because the program name is in passed as an argument.

See:

What does int argc, char *argv[] mean?

You also didn't declare int blk in the correct scope, when you leave that if(argc == 0) statement, the variable blk will disappear.