简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM