argv的用法, 请高手看看错在哪里
/* hello_to.c */
#include <stdio.h>
int main(int argc, char *argv[])
{
int i,j;
int index;
char name[100];
index = 0;
for(i = 1; i <3; i++){
for(j = 0; argv[i][j] != '\0 '; j++)
name[index++] = argv[i][j];
if(i ==2)
name[index] = '\0 ';
else
name[index++] = ' ';
}
printf( "Hello, %s.\n ", name);
return 0;
}
运行结果:
$./hello_to Michele
error.....
如果 $./hello_to Michele Lee
输出 Hello, Michele Lee.
我想要输入 $./hello_to Michele 也能正常运行, 输出 Hello, Michele 该怎么改??
[解决办法]
for(i = 1; i <3; i++)
===>
for(i = 1; i <argc; i++)