正如标题中所述,我有一个函数“load”,它返回一个包含 2 个 int 和一个字符数组的结构。该数组是一维的,看起来像: [################ ### $ $ ### # .# ### 。 #.@##$# * ### ###############] 我想将其打印为:
##########
###### ##
# $ $ ##
# # .# ##
# . #.@ #
##$# * #
## ######
##########
知道这里的宽度是10。 所以我的问题是如何每 10 个字符设法返回到下一行......? 这是我到目前为止所写的内容:
void print_map(map map_loaded){
char *p=map_loaded.p_char;
int height=map_loaded.height;
for(int i=0; i<height; i++){
printf("%c", *p[i]);
}
如你所见,我只知道如何阅读每个字符,但我不知道如何返回行... 如果有人可以阅读我的代码并向我提供他/她的反馈,我将非常感激...提前致谢!
最佳答案
由于您所需要的只是每 10 个字符后换行,因此只需检查自上次添加新行以来是否已打印 10 个字符即可。
void print_map(map map_loaded){
char *p=map_loaded.p_char;
int height=map_loaded.height;
for(int i=0; i<height; i++){
printf("%c", p[i]);
if( (i+1)%10==0)
printf("\n");
}
关于从一维数组创建二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55029434/