How does C Allocate Memory of Data Elements in a Multidimensional Array?

This C Tutorial Explains how Elements in a Multidimensional Array in C are Stored in Memory with an Example.

A multidimensional array can be thought of as a one-dimensional array with composite elements. For example:

  int flags[3][2][5][4]= {
  {
   {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}},
   {{21,22,23,24},{25,26,27,28},{29,30,31,32},{33,34,35,36},{37,38,39,40}}
  },
  {
   {{41,42,43,44},{45,46,47,48},{49,50,51,52},{53,54,55,56},{57,58,59,60}},
   {{61,62,63,64},{65,66,67,68},{69,70,71,72},{73,74,75,76},{77,78,79,80}}
  },
  {
   {{81,82,83,84},{85,86,87,88},{89,90,91,92},{93,94,95,96},{97,98,99,100}},
   {{101,102,103,104},{105,106,107,108},{109,110,111,112},{113,114,115,116},
                                                         {117,118,119,120}}
  }
                          };

Let’s presume that array ‘flags’ have three composite elements and looks like as:

    flags[3][2][5][4] = {*, *, *};

Each composite element ‘*’ is an array of 2 arrays of 5 arrays of 4 integers. Since, elements in one-dimensional array are stored in contiguous memory locations therefore with the above presumption, composite elements in ‘flags’ stored in continuous memory locations. This organisation of array elements in memory is called row major order.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.