char **argv[]
Wrong. It should be either char **argv or char *argv[] , and not a mixture of both. And then it becomes a pointer to a pointer to characters, or rather a pointer to c-lines, i.e. An array of c-lines. :) cdecl.org is also quite useful in such a thing.
Then, for access, of course. Easy, well, access to it. :) argv[0] will be the first line, argv[3] will be the fourth line. But I totally do not recommend replacing material in an array that does not belong to you, or that you know its insides.
When resizing an array, as you write C ++, use std::vector , which makes all the distribution difficult for you and really safe. This usually depends on the type of array. Dynamically allocated arrays ( int* int_arr = new int[20] ) can, static arrays ( int int_arr[20] ) cannot.
To copy everything in argv into one std::string , go through the array and add each c-string to your std::string . I would not recommend this, rather, have std::vector<std::string> , i.e. An array from std::strings , each of which contains one of the arguments.
std::vector<std::string> args; for(int i=0; i < argc; ++i){ args.push_back(argv[i]); }
In the last paragraph, since the standard requires argv to complete with a NULL pointer, this is pretty simple.
int myargc = 0; char** argv_copy = argv; while(++argv_copy) ++myargc;
while(++argv_copy) first increment the array pointer, pointing to the next element (for example, after the first iteration, it will point to c-string # 2 ( argv[1] )). After that, if the pointer evaluates to false (if it's NULL ), then the loop stops and you have myargc . :)
Xeo
source share