0

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>
int 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]);
            }
        }
    }
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 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 Apr 15 '14 at 18:16
  • 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 Apr 15 '14 at 18:23

2 Answers2

3

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 *.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Thank you for the explanation. One further question, I need to pass in a char as well. Will this implementation allow for that? – InfoSecNoob Apr 15 '14 at 18:47
0

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.

Community
  • 1
  • 1
GriffinG
  • 662
  • 4
  • 13
  • The C spec says "The value of argc shall be nonnegative." and further talks about "If the value of argc is greater than zero". `argc` can be 0, but maybe not on platforms you have experienced. – chux - Reinstate Monica Apr 15 '14 at 18:26
  • Certainty not on anything I've seen, but if he's trying to read arguments, then argc should be non-zero. It's counter intuitive to check that the number of arguments is equal to zero and then proceed to use the arguments. – GriffinG Apr 15 '14 at 18:28
  • Agree about "It's counter intuitive ...", but "never even be possible" for "argc == 0" overstates and does not agree with the C spec. – chux - Reinstate Monica Apr 15 '14 at 18:32
  • Just for the sake of clarity, `argv` is not a character array. It is a pointer to the first element of an array of pointers (of type `char *`). These pointers, in turn, are pointers to character arrays. I know that probably seems like a minor nit to pick, but given the fact that you're trying to explain `argv` to someone who is seemingly confused about how `argc` and `argv` work, it seems appropriate to give a precise description. – Mike Holt Apr 15 '14 at 18:34