in DS
518 views
0 votes
0 votes

Consider the following declaration of a two dimensional array in C:

char a[1000][40];

Assuming that the main memory is byte addressable and that the array is stored starting from address 0, the address of a[40][10] is _____

in DS
by
518 views

2 Answers

3 votes
3 votes
Best answer
C uses row-major order for arrays and indexing starts from 0 and size of a char is 1 byte. So, before a[40][10], we have 40 rows of 40 bytes = 1600 bytes plus 10 chars of 1 byte each, which occupies memory locations 0-1599, 1600-1609. So, a[40][10] strats from address 1610.
selected by
by
1 vote
1 vote

To reach a[40][10]

Skip 40 rows, and 10 columns for RMO (default in C)

=> Skip 40(size of row) elements + 10 elements.

Now, size of a row = number of total columns.

=> Skip 40(40) + 10 elements.

=> Skip 1610 elements.

 

Now, 1 element = 1B, because the element is char.

=> After 1610B is the starting address of a[40][10]



Bonus:-

Type Storage size  
char 1 byte  
unsigned char 1 byte  
signed char 1 byte  
int 2 or 4 bytes  
unsigned int 2 or 4 bytes  
short 2 bytes  
unsigned short 2 bytes  
long 8 bytes  
unsigned long 8 bytes  

Source

Answer:

Related questions