I am trying to convert a char stored in the argv array to int, using the atoi function, I am new to C++ and passing arguments, so please bear with me.
Every time I attempt to set the value stored at atoi[1] to a const char, and print the char, it doesn't print anything at all. If I am doing this wrong it should still print a memory address, right?
I am setting it to a const char because the atoi function needs a const char as it's argument. (Please correct me if I am wrong)
I have been doing this via ideone.com, so my argv array isn't really set at runtime, since ideone doesn't allow that.
Here is my code so far:
#include <iostream>
#include<string>
#include <stdlib.h>
using namespace std;
int main() {
int argc;
char *argv[4];
*argv[0] = 'x';
*argv[1] = '5';//this will be the length of our first array for testing
*argv[2] = '3';//this will be the length of our second array for testing
*argv[3] = '4';//this will be the length of our third array for testing
argc = 4;
const char * argv1 = argv[1];
cout << "*argv[1]: " << *argv[1] << endl;
cout << "argv[1]: " << argv[1] << endl;
cout << *argv1;
//int x = atoi(argv1);
return 0;
}
UPDATED CODE: Now featuring the ability to compile!
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main() {
char nullGuy = NULL;
char * nullptr1 = &nullGuy;//Null ptr
int argc;
const char * argv[5];
argv[0] = "x";
argv[1] = "5";
argv[2] = "3";
argv[3] = "4";
argv[4] = nullptr1;
argc = 4;
int x = atoi(argv[1]);
cout << "argv[1]: " << argv[1] << endl;
cout << "x: " << x << endl;
return 0;
}