I realize that each argument passed from the command line is stored as a string in 'char *argv[]'
I'd like to pass
$ progname array[500]
and pull the string from argv[1] subsequently specifying an array based on what I read in.
Any ideas?
I realize that each argument passed from the command line is stored as a string in 'char *argv[]'
I'd like to pass
$ progname array[500]
and pull the string from argv[1] subsequently specifying an array based on what I read in.
Any ideas?
Well from your new comment, I will try to answer the question. I am doing most of this to show you one possibility, my code will have some makeshift string parsing and not much error checking, it's just to give you an idea of how you can do it.
If you wanted the user to specify the size of an array you wanted to make, that is doable.
If you run the above program and pass it argument
500
, it would allocate a memory block to store up to 500 *sizeof(char)
.If you wanted the user to actually give you the name of the array and its size, that's a little different - you can not create a variable name at runtime. One thing you could do is map strings to arrays.
Here's a program that would accept an argument list consisting of arguments like
hello[100]
and create a mapping ofhello
to a pointer to a block of space for 100 elements.If you ran the above code and passed it a list of arguments, such as
hello[500] array[1000] somelist[100]
, it would create 3 heap-allocated blocks and map names to them:"hello"
would map to a pointer to a block of500 * sizeof(char)
,"array"
would map to a pointer to a block of1000 * sizeof(char)
, and"somelist"
would map to a pointer to a block of100 * sizeof(char)
bytes.There is a problem with that code above though - it does not keep track of the number of bytes allocated for each pointer's pointed-to block, which makes it rather worthless for you. One alternative would be to map the names to
boost::array<Typename T, std::size_t n>
, which will be boost's fixed-sizearray
class that keeps track of its size and has asize()
member function.