程序具有不确定的行为,因为你没有初始化指针ss
和分配内存你要去哪里复制通过argv的元素指向
char **ss; // What value does it have?
for(int i=0;i<argc;i++){
ss[i] = argv[i];
你可以做下面的方式
char **ss = new char *[argc];
for(int i=0;i<argc;i++){
ss[i] = argv[i];
更好的方法是使用std::vector<std::string>
。在这种情况下,您不仅可以复制指向参数的指针,还可以复制参数。例如
#include<iostream>
#include <vector>
#include <string>
int main(int argc , char **argv)
{
std::vector<std::string> v(argv, argv + argc);
for (const std::string &s : v) std::cout << s << std::endl;
return 0;
}
如果你的编译器不支持基于for语句的范围内,那么你可以用它替换
for (std::vector<std::string>::size_type i = 0; i < v.size(); i++)
{
std::cout << v[i] << std::endl;
}
请大家习惯用的所有警告和调试信息(即'G ++ -Wall编译-g')并学习如何使用'gdb'调试器。 –