gpt4 book ai didi

c - strcpy 从一个字符串数组到另一个字符串数组

转载 作者:行者123 更新时间:2023-11-30 17:35:54 26 4
gpt4 key购买 nike

所以我创建了一个常量名称字符串数组

  const char *players[10];
players[0] = "Anselm";
players[1] = "Otto";
players[2] = "Fedor";
players[3] = "Juergen";
players[4] = "Ulrich";
players[5] = "Eugen";
players[6] = "Meinrad";
players[7] = "Gotthard";
players[8] = "Frank";
players[9] = "Matthaeus";

我希望能够将任何一个玩家从这个字符串数组复制到另一个

这个数组:

char waiting[10][20]; 
strcpy(waiting[0], players[2]);

但是由于在这种情况下我不能使用 strcpy (我相信),所以我对如何做到这一点有点不知所措?

最佳答案

以下内容应该有效。但是,您应该熟悉 malloc() 的使用。

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[])
{

const char *players[10];
players[0] = "Anselm";
players[1] = "Otto";
players[2] = "Fedor";
players[3] = "Juergen";
players[4] = "Ulrich";
players[5] = "Eugen";
players[6] = "Meinrad";
players[7] = "Gotthard";
players[8] = "Frank";
players[9] = "Matthaeus";

char waiting[10][20];
for (int i = 0; i < 10; i++){
strcpy(waiting[i], players[i]);
}
for (int i = 0; i < 10; i++){
printf("waiting[%d]: %s\n", i, waiting[i]);
}

return 0;
}

输出:

waiting[0]: Anselm
waiting[1]: Otto
waiting[2]: Fedor
waiting[3]: Juergen
waiting[4]: Ulrich
waiting[5]: Eugen
waiting[6]: Meinrad
waiting[7]: Gotthard
waiting[8]: Frank
waiting[9]: Matthaeus
Program ended with exit code: 0

关于c - strcpy 从一个字符串数组到另一个字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22851525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com