C 初学者:如何使用 while 循环将参数从 *argv[] 复制到字符串数组?
C beginner: How to copy the arguments from *argv[] to an array of strings using a while loop?
我正在学习 C,我是一个绝对的初学者。我正在尝试做一个练习,但经过几次试验后我无法解决它。
我必须使用 while
循环 将 *argv[]
中的参数复制到字符串数组 *states[]
所以每个
printf("You've mentioned the state %s.\n", states[i]);
打印出我应该作为参数输入的状态名称。我认为 我必须使用的唯一 header 是 stdio.h
。
预先感谢您的帮助。
编辑。到目前为止,我只尝试过这样的事情
char *states[];
i = 1;
states[i] = argv[i];
if(argc > 1) {
while(i < argc){
printf("You've mentioned the state %s.\n", states[i]);
i++;
}
}
但是,当然,由于 states[i] = argv[i];
语句,我得到了一个错误。
你可以这样做:
char* states[argc];
int i = 0;
while (i < argc) {
strcpy(states[i], argv[i]);
}
你必须包括
我正在学习 C,我是一个绝对的初学者。我正在尝试做一个练习,但经过几次试验后我无法解决它。
我必须使用 while
循环 将 *argv[]
中的参数复制到字符串数组 *states[]
所以每个
printf("You've mentioned the state %s.\n", states[i]);
打印出我应该作为参数输入的状态名称。我认为 我必须使用的唯一 header 是 stdio.h
。
预先感谢您的帮助。
编辑。到目前为止,我只尝试过这样的事情
char *states[];
i = 1;
states[i] = argv[i];
if(argc > 1) {
while(i < argc){
printf("You've mentioned the state %s.\n", states[i]);
i++;
}
}
但是,当然,由于 states[i] = argv[i];
语句,我得到了一个错误。
你可以这样做:
char* states[argc];
int i = 0;
while (i < argc) {
strcpy(states[i], argv[i]);
}
你必须包括