如何打印数组? [英] How to print the array?

查看:220
本文介绍了如何打印数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main() {  
  int my array[3][3] =
    10, 23, 42,    
    1, 654, 0,  
    40652, 22, 0  
  };  

  printf("%d\n", my_array[3][3]);  
  return 0;
}

我不能够获得阵列打印..任何想法,为什么?我是一个开始,程序员,所以建议任何话是AP preciated。

I am not able to get the array to print.. Any ideas why? I am a beginning programmer so any words of advice are appreciated.

推荐答案

你在做什么是印刷在现场数组中的值[3] [3],这是一个3by3阵列失效,你需要循环所有的斑点和打印。

What you are doing is printing the value in the array at spot [3][3], which is invalid for a 3by3 array, you need to loop over all the spots and print them.

for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 3; j++) {
        printf("%d ", array[i][j]);
    }
    printf("\n");
} 

这将打印的格式如下

10 23 42
1 654 0
40652 22 0

如果您想了解更多详细的格式,你必须改变的printf是如何格式化。

if you want more exact formatting you'll have to change how the printf is formatted.

这篇关于如何打印数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆