[英]How do I print int into array variable of char type in C?
Input maze: 输入迷宫:
##..##############
##..............##
########..########
##........##....##
##..########..####
Output expected: 预期输出:
##00##############
##++02++04++06++##
########++########
##++08++06##----##
##10########--####
here I have stored maze as 2d array into structure as: 在这里,我将迷宫作为2d数组存储到结构中,如下所示:
struct mazecells{
char symbol;
int reachable;
int visited;
int clear;
int costs;
};
typedef struct maze {
struct mazecells M[BUFFERSIZE][BUFFERSIZE];
int startx, starty;
int numrows, numcolumns;
int initdir;
}maze_t
so whenever my program traverse through maze path
ie '.'
因此,每当我的程序经过
maze path
即'.'
, I have assigned cost to that path using maze->M[pos.y][pos.x].costs = <cost_value>
. ,我已使用
maze->M[pos.y][pos.x].costs = <cost_value>
将成本分配给该路径。
Now while printing output as shown above, I need to print the cost value instead of maze symbol. 现在,在如上所述打印输出时,我需要打印成本值而不是迷宫符号。 I have written below code but it is printing some random junk characters.
我已经写了下面的代码,但是它正在打印一些随机的垃圾字符。 How can I achieve it.
我该如何实现。
if (maze->M[i][j].costs !=0)
{
printf("%c", '0');
printf("%c", maze->M[i][j].costs);
}
it's giving below output: 它给出下面的输出:
##++##############
##0☺0☻0♥0♦0♣0♠0##
########0♣########
##0 00♠##....##
##++########..####
If you want to translate the number in "cost" to string, you must use one of: 1. Use %d instead of %c. 如果要将“ cost”中的数字转换为字符串,则必须使用以下之一:1.使用%d代替%c。 2. Use the ascii table and convert your number to char.
2.使用ascii表并将您的数字转换为char。 3. Use %s with itoa funtion.
3.将%s与itoa功能一起使用。 Actually you were printing ascii code of the number (cost) but not the number
实际上,您正在打印数字(成本)的ASCII代码,而不是数字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.