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
我需要声明一个2d数组来表示棋盘的大小。但是,我很难理解如何实际计算电路板的宽度和长度。我想知道如何计算我的2d数组的行和列的大小
Say, int boardSize[5][5]
?
说,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 //
}
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!
对不起,因为没有具体的乞讨。那么,在这里我需要计算boardwidth和boardheight变量?我如何从上面声明的数组中做到这一点。谢谢!
4 个解决方案
#1
8
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]
.
boardSize [0]为您提供矩阵的第一行,boardSize [0] [0]是其第一个元素。因此,您要查找的数量是boardSize / sizeof boardSize [0]和sizeof boardSize [0] / sizeof boardSize [0] [0]。
BTW: use size_t
as a type for sizes, not int
.
顺便说一句:使用size_t作为大小的类型,而不是int。
#2
0
If you say
如果你说
int boardSize[5][5];
that gives you a 2D array of integers, 5 by 5 in size. So it will have 5 rows of 5 columns each, for a total of 25 integers.
它为您提供了一个2D整数的数组,大小为5 x 5。因此它将有5行,每列5列,总共25个整数。
#3
0
printf("Size of the board in bytes %d\n", sizeof(boardSize));
printf("Size of column size in bytes %d\n", 5*sizeof(int));
printf("Size of row in bytes %d\n", 5*sizeof(int));
If you want leave the board as statically allocated the declare it as global like,
如果你想离开董事会作为静态分配声明它像全球一样,
static int board[5][5];
Then traverse it in your "createBoard" method(I hate Hungarian notations) for correct initialization :
然后在你的“createBoard”方法(我讨厌匈牙利语符号)中遍历它以进行正确的初始化:
for(i = 0; i < hight; i++)
for(j = 0;j< width; j++)
board[i][j] = <initialization stuff>
or you can dynamically allocate it in your createBoard() method. In that case do not declare as local variable for main.
或者您可以在createBoard()方法中动态分配它。在这种情况下,不要声明为main的局部变量。
int * createBoard(int hight, int width){
int * board;
if(board = malloc(sizeof(int) * hight * width))
return board;
return NULL;
}
in main() you can do something like this:
在main()中,您可以执行以下操作:
int * board = createBoard(5,5);
if(!board)
printf("Allocation failure \n");
exit(EXIT_FAILURE);
#4
0
This is easily fixed with the removal of all mysterious magic numbers from your code.
通过从代码中删除所有神秘的幻数,可以轻松解决这个问题。
#define BOARD_WIDTH 8
#define BOARD_HEIGHT 8
square_t board [BOARD_WIDTH][BOARD_HEIGHT];
createBoard (board, BOARD_WIDTH, BOARD_HEIGHT);
void createBoard (square_t* board, int boardWidth, int boardHeight)
{
}
#1
8
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]
.
boardSize [0]为您提供矩阵的第一行,boardSize [0] [0]是其第一个元素。因此,您要查找的数量是boardSize / sizeof boardSize [0]和sizeof boardSize [0] / sizeof boardSize [0] [0]。
BTW: use size_t
as a type for sizes, not int
.
顺便说一句:使用size_t作为大小的类型,而不是int。
#2
0
If you say
如果你说
int boardSize[5][5];
that gives you a 2D array of integers, 5 by 5 in size. So it will have 5 rows of 5 columns each, for a total of 25 integers.
它为您提供了一个2D整数的数组,大小为5 x 5。因此它将有5行,每列5列,总共25个整数。
#3
0
printf("Size of the board in bytes %d\n", sizeof(boardSize));
printf("Size of column size in bytes %d\n", 5*sizeof(int));
printf("Size of row in bytes %d\n", 5*sizeof(int));
If you want leave the board as statically allocated the declare it as global like,
如果你想离开董事会作为静态分配声明它像全球一样,
static int board[5][5];
Then traverse it in your "createBoard" method(I hate Hungarian notations) for correct initialization :
然后在你的“createBoard”方法(我讨厌匈牙利语符号)中遍历它以进行正确的初始化:
for(i = 0; i < hight; i++)
for(j = 0;j< width; j++)
board[i][j] = <initialization stuff>
or you can dynamically allocate it in your createBoard() method. In that case do not declare as local variable for main.
或者您可以在createBoard()方法中动态分配它。在这种情况下,不要声明为main的局部变量。
int * createBoard(int hight, int width){
int * board;
if(board = malloc(sizeof(int) * hight * width))
return board;
return NULL;
}
in main() you can do something like this:
在main()中,您可以执行以下操作:
int * board = createBoard(5,5);
if(!board)
printf("Allocation failure \n");
exit(EXIT_FAILURE);
#4
0
This is easily fixed with the removal of all mysterious magic numbers from your code.
通过从代码中删除所有神秘的幻数,可以轻松解决这个问题。
#define BOARD_WIDTH 8
#define BOARD_HEIGHT 8
square_t board [BOARD_WIDTH][BOARD_HEIGHT];
createBoard (board, BOARD_WIDTH, BOARD_HEIGHT);
void createBoard (square_t* board, int boardWidth, int boardHeight)
{
}