You need to allocate memory for rArray
and also need to initialise the outer loop counter i
.
Since the contents of argv
are constant strings, you could just copy pointers to them
rArray = new char*[argc+1];
for(int i=0; i <= argc; i++) {
rArray[i] = argv[i];
}
// use rArray
delete [] rArray;
Note that argv[argc]
is guaranteed to be NULL
. I've updated the loop to copy this as well (hence the unusual looking i<=argc
exit condition)
If you really want to copy the content of the strings (as minitech suggests), the code becomes a bit more complicated:
rArray = new char*[argc+1];
for(int i=0; i < argc; i++) {
int len = strlen(argv[i]) + 1;
rArray[i] = new char[len];
strcpy(rArray[i], argv[i]);
}
rArray[argc] = NULL;
// use rArray
for(int i=0; i < argc; i++) {
delete [] rArray[i];
}
delete [] rArray;