如何在C中组合两个不同数据类型的变量?
所以基本上我在为一场比赛写一段代码,我有两个变量,球员编号和球员姓名,我如何配对/组合这两个变量如何在C中组合两个不同数据类型的变量?,c,C,所以基本上我在为一场比赛写一段代码,我有两个变量,球员编号和球员姓名,我如何配对/组合这两个变量 #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++)
#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;
}
#包括
#包括
#包括
#定义参赛者16
内部主(空)
{
字符数组[参赛者][20];
int n;
对于(n=0;n使用struct)
#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;
}
#包括
#包括
#包括
#定义参赛者16
typedef结构播放器{
整数;
字符名[20];
}玩家;
内部主(空)
{
玩家阵列[参赛者];
int n;
对于(n=0;n使用structs存储关于参赛者的信息:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CONTESTANTS_LEN 16
#define NAME_LEN 20
struct Contestant {
int contestant_id;
char contestant_name[NAME_LEN];
};
int main(void)
{
struct Contestant contestants[CONTESTANTS_LEN];
int i;
for(i=0;i<CONTESTANTS_LEN;i++) {
printf("Player %d: ", i+1);
contestants[i].contestant_id = i+1;
scanf("%s", &(contestants[i].contestant_name));
}
return 0;
}
#包括
#包括
#包括
#定义参赛者\u LEN 16
#定义名称\u LEN 20
结构选手{
int参赛者id;
char参赛者姓名[姓名];
};
内部主(空)
{
结构参赛选手[参赛选手];
int i;
对于(i=0;i配对/组合是什么意思?可以使用struct将不同的数据类型“打包”在一起。fflush(stdin)
导致UB,不是吗?请参阅。最好避免。您已经通过将名称存储在数组中来对它们进行配对。数组索引加1是玩家编号。同样,玩家编号减1是该玩家姓名的数组索引。然后如何打印玩家数组的第6个元素,以同时打印玩家编号a一起命名吗?谢谢你的帮助,顺便说一句,非常感谢useful@kallumallenprintf(“播放器编号为%d\n播放器%s\n的名称”,数组[5]。数字,数组[5]。名称);