0

I have the following code below:

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

  int n = argv[1];
  function1(n);

}

Naturally, my code gives an error because argv[] is a pointer to a char. I want to run the program with an integer that gets passed and used by a function called "function1." For example:

./program 5

How do I do this with any input, so that the n is equal to that input?

  • 3
    Does this answer your question? [How can I get argv\[\] as int?](https://stackoverflow.com/questions/9748393/how-can-i-get-argv-as-int) – gkhaos Mar 02 '21 at 16:02
  • 2
    argv is not a pointer to a char, but am array of char*, which are strings. So you need a function that converts char* to int, google is your friend from here – DownloadPizza Mar 02 '21 at 16:04
  • You can use atoi() to convert argv[1] to int – Raz K Mar 02 '21 at 16:04

0 Answers0