2d阵列行/列长度C [英] 2d array rows/columns length C

查看:108
本文介绍了2d阵列行/列长度C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要声明一个2d数组来表示棋盘的大小.但是,我在理解如何实际计算板的宽度和长度时遇到了麻烦. 我想知道如何计算二维数组的行和列的大小

I need to declare a 2d array to represent the size of chess-board. However, I'm having a trouble understanding how would I actually calculate the width and the length of the board. I would like to know how could I calculate size of the rows and columns of my 2d array

说,int boardSize[5][5]?

int main()
{
   int boardSize[5][5];
   int boardWidth=?
   int boardHeight =?
   createBoard(boardWidth, boardHeight);
}

 int createBoard(int width, int height)
 {
   // code that actually creates board //
 }

对不起,因为在乞讨中没有具体说明.那么,在这里我需要计算boardwidth和boardheight变量吗?我如何从上面声明的数组做到这一点.谢谢!

Sorry, for not being specific in the begging. So, here I need to calculate boardwidth and boardheight variables? How do I do that from the declared array above. Thank you!

推荐答案

boardSize[0]为您提供矩阵的第一行,boardSize[0][0]为其元素的第一行.因此,您要查找的数量是sizeof boardSize/ sizeof boardSize[0]sizeof boardSize[0]/ sizeof boardSize[0][0].

boardSize[0] gives you the first row of the matrix, boardSize[0][0] the first of its elements. So the quantities you are looking for are sizeof boardSize/ sizeof boardSize[0] and sizeof boardSize[0]/ sizeof boardSize[0][0].

顺便说一句:使用size_t作为尺寸类型,而不是int.

BTW: use size_t as a type for sizes, not int.

这篇关于2d阵列行/列长度C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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