Dynamic 2d array into a struct

May 21, 2016 at 8:52pm
Hello.
How can i add a dynamic 2d array into a struct?
Last edited on May 22, 2016 at 5:10am
May 21, 2016 at 10:43pm
You can't dynamically allocate a 2D array in one step like 'new int[rows][cols]'. Instead you either need to allocate a 1D array with rows*cols elements and do maths to convert a row and col into an index of the 1D array or you need to allocate an array of pointers, where each pointer points to an array holding the data. To hold the array of pointers you need a pointer to a pointer, so you need to make examsptr an int**.

You then need to allocate the arrays that are pointed at by the array of pointers in a loop.
May 22, 2016 at 5:10am
Any example guys? I'm very new with pointers.
May 22, 2016 at 5:20am
closed account (48T7M4Gy)
http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new
Topic archived. No new replies allowed.