从一维数组创建二维数组

标签 c arrays 2d

正如标题中所述,我有一个函数“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/

相关文章:

Java - 二维数组 - 不能使用增强循环?

c - 什么是特权指令?

c++ - printf( ) 的内存消耗

c - 使用 fopen() 打开的文本文件的内容是否会在堆栈中?

java - 将二维数组复制到一维数组

c - 如何使用指针和数组访问嵌套结构?

c++ - LibCurl Unicode 数据

arrays - 在 Julia 中将整数数组转换为字符串数组

c++ - Bresenham 的中点算法,我如何填补空白?

java - lwjgl 用相机创建一个二维世界