C初学者: How to copy the arguments from *argv[] to an array of strings using a while loop?

标签 c arrays while-loop copy argv

我正在学习 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/

相关文章:

计数器和累加器不工作并导致程序崩溃。我究竟做错了什么?

python - 修复 python/Bioinformatics 中的代码

c++ - 从 char 中删除转义字符

c - 指向 void* 的指针,以及通用的冒泡排序

java - 查找数组中整数的模式

c - 将文件中的单词存储到 C 中的数组中

mysql - 如何构建像 FourSquare 一样的排行榜(高于和低于你的用户)

c++ - 在 C++ 中放松 void * 转换

c - 如何知道应用程序的启动点

php - PHP while 循环的第一行不回显到屏幕