Computer Science Canada A question about 2D arrays |
Author: | HazySmoke)345 [ Sun Oct 31, 2010 10:12 am ] | ||||||||
Post subject: | A question about 2D arrays | ||||||||
So, to the best of my understanding, there are two TYPES of 2D arrays.
In this case, a is an array of array, all of the 12 elements are stored in a continous chunk. But b is an array of pointers, all the rows are stored in different places. If I want, I can even make the rows to have different lengths. We access the 2D arrays the same way, but how they are laid out in memory is a little different. Here's my first question. Can I create the Type 1 2D array dynamically, using malloc? I think the answer is yes, but someone please confirm that I'm doing this right:
Also, why doesn't this give compile errors?
Note that I did not specify the column width in foo, You'd think it should be like this
And lastly, what's the real difference between MALLOC and CALLOC? I get the impression that we *should* use malloc for creating single objects and calloc for arrarys, but time and time again I see people creating arrays using malloc. |
Author: | OneOffDriveByPoster [ Wed Nov 03, 2010 4:41 pm ] | ||
Post subject: | Re: A question about 2D arrays | ||
|