int *temp = new int[atoi(argv[3])];
what does the above statement mean ?Please explain in detail .
int *temp = new int[atoi(argv[3])];
what does the above statement mean ?Please explain in detail .
It allocates a int array with elements of the 3 program argument.
for example: main.exe foo bar 100
will allocated a array with 100 integers.
you declare a pointer to a newly allocated array of integers with the size determined by the numeric value that the string argv[3] (i.e. the 3rd command line argument) has
parses the string argv[3] into an int, then creates an array of ints of that size. Then it assigns the pointer to (the first element of) that array to temp.
In Detail:
int* temp //declares a pointer to int
int* temp = //that points to
int* temp = new //newly allocated memory
int* temp = new int [ //of type array of int of size
int* temp = new int [ atoi ( //equal to the integer representation (atoi -> alphabetical to integer)
int* temp = new int [ atoi ( argv[3] //of the third command line argument passed to your program.
That is if your third command line arg is "5", temp will point to a dynamically allocated array of 5 ints
Pointers? New? Not very C++ish...
#include <vector>
#include <boost/lexical_cast.hpp>
int main(int argc, char** argv)
{
std::vector<int> temp(boost::lexical_cast<int>(argv[3]));
}