当前位置: 动力学知识库 > 问答 > 编程问答 >

the size of two dimensional array in C

问题描述:

I have the following two dimensional array:

#define ROW 100

#define LINE 50

int a[ROW][LINE];

but how to get the sizeof array for the last 45 rows, for example a[55][0] to a[99][99]?

can we do something like sizeof(&a[55])?

网友答案:

sizeof of an array is guaranteed to be equal to the sizeof of a single element multiplied by the number of elements. So, if you want to know how much memory is occupied by a specific number of rows, just calculate the number of elements and multiply by element size. 45 rows would require

45 * LINE * sizeof a[0][0]

Or, alternatively

45 * sizeof a[0]

It doesn't really matter whether these are last 45 rows. All rows are the same.

网友答案:

If you want just the last lines, from line 55 and forward, how about

(ROW - 55) * LINE * sizeof(int)

If you just want a generic N number of lines, then how about

N * LINE * sizeof(int)
分享给朋友:
您可能感兴趣的文章:
随机阅读: