gpt4 book ai didi

c - 如何在C中组合两个不同数据类型的变量?

转载 作者:行者123 更新时间:2023-11-30 19:18:15 25 4
gpt4 key购买 nike

所以基本上我正在为锦标赛编写一段代码,并且我有两个变量:玩家编号和玩家姓名,我将如何配对/组合这两个变量?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define CONTESTANTS 16

int main(void)
{
char array[CONTESTANTS][20];
int n;



for(n=0;n<CONTESTANTS;n++)
{
printf("Player %d: ", n+1);
scanf("%s", array[n]);
fflush(stdin);
}

return 0;
}

最佳答案

使用结构。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define CONTESTANTS 16

typedef struct player {
int number;
char name[20];
} Player;

int main(void)
{
Player array[CONTESTANTS];
int n;

for(n=0;n<CONTESTANTS;n++)
{
printf("Player %d: ", array[n].number = n+1);
scanf("%19s", array[n].name);
}

return 0;
}

关于c - 如何在C中组合两个不同数据类型的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26982462/

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