Reputation: 123
Why is something so seemingly simple is crashing my program?
I am trying to get a value for n
to make an array the size of N
and perform various operations on it, but that's beside the point. Anyways, It keeps crashing every time I try to access argv[1]
.
int main(int argc, char * argv[])
{
int n;
n = atoi(argv[1]); //Crashes here!
cout << "\nN: " << n << endl;
}
Upvotes: 0
Views: 1321
Reputation: 441
Does argv[1] exist? To prevent your code from accessing memory it should not check how many arguments were passed.
if(argc >= 2)
n = argv[1];
else
std::cout << "Proper usage: .....\n";
This seems like a great time to learn how to use your debugger to view the contents of argv and argc.
Upvotes: 1