我需要将数字转换为字符串。我是发牌员,负责向玩家分发牌。这副牌包含 40 张牌。四粒种子。
#include <stdio.h>
#include <conio.h>
#include <string.h>
char *convertNum(int a); //initialization function
void main(){
char *car=convertNum(30);
printf(" number to convert 30 : %s", car);
getch();
}
char *convertNum(int a){
char *seed, *value;
switch ((a-1)/10){
case 0: seed="C"; break;
case 1: seed="Q"; break;
case 2: seed="F"; break;
case 3: seed="P"; break;
default: seed="X"; break;
}
switch(a%10){
case 0: value="10"; break;
case 1: value="01"; break;
case 2: value="02"; break;
case 3: value="03"; break;
case 4: value="04"; break;
case 5: value="05"; break;
case 6: value="06"; break;
case 7: value="07"; break;
case 8: value="08"; break;
case 9: value="09"; break;
default: value="XX"; break;
}
strcat(value, seed); /* ERROR: I need to concatenate the strings value and seed, but this isn't the right way */
return value;
}
如何连接字符串? value
和 seed
是指针。
最佳答案
1.指针*value和*seed未初始化。他们没有内存。您需要使用 malloc() 或 calloc() 以及一些内存块来初始化这些指针,然后它才能保存值。
希望这对你有用
关于c - 为什么 strcat 连接两个字符串失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34787756/