Bigby
Bigby

Reputation: 303

Retrieving Integers from an Array of Chars

The program I am working is passed 3 command line arguments, all of which should be integers. the skeleton program that was provided has:

int main(char *argv[]){
...
}

What I have done the provided is just try to set an integer to the item in each position of the array (0, 1, & 2), but it won't compile since its trying to convert a char to an integer. I'm sure there is an easy fix for this, but I can't seem to figure it out.

Upvotes: 1

Views: 77

Answers (3)

tmaric
tmaric

Reputation: 5477

Try the cstdlib function "atoi" for each argument:

char* to int conversion

Upvotes: 0

Roman Bataev
Roman Bataev

Reputation: 9361

Try something like this:

int i = atoi(argv[1]);

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

Since this looks like homework, I'll give you several of hints:

  • Signature of main() includes an int argument, conventionally named argc
  • The initial argument is argv[1], not argv[0]
  • atoi is the easiest way to convert a string to an integer.

Upvotes: 5

Related Questions