Suppose I have a program like this:
#include <iostream>
#include <string>
#include <vector>
// Takes values and outputs a string vector.
std::vector<std::string> foo(const int argc, char* args[]) {
std::vector<std::string> strings;
for (int i = 0; i < argc; i++)
strings.push_back(args[i]);
return strings;
}
int main(int argc, char *args[]) {
std::vector<std::string> strings = foo(argc, args);
for (unsigned int i = 0; i < strings.size(); i++)
std::cout << strings[i] << std::endl;
return 0;
}
Where the takeaway is that I'm trying pass the main() function's char** argument to another function or class. (I understand there are better ways to achieve what the above program does, my question is about passing char** arguments as read-only).
Questions:
- I've found that I can't make the second foo() argument const like the first. Why is this? A char** can't be converted to a const char**?
- I want to pass in this argument as "read-only". I'm not sure how to go about this, if it were say a string I'd pass it in via const reference, but I'm not sure how to go about this with pointers?
std::vector<std::string> args(argv+1, argv+argc);
(argv+1
to skip the program name) – Afterdamp