我正在学习 C,而且我是一个绝对的初学者。我正在尝试做一个练习,但经过几次尝试后我无法解决它。
我必须复制 *argv[]
中的参数到字符串数组 *states[]
使用while
循环所以每个
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初学者: How to copy the arguments from *argv[] to an array of strings using a while loop?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47660352/