Does someone know how I can use dynamically allocated multi-dimensional arrays using C? Is that possible?
Does someone know how I can use dynamically allocated multi-dimensional arrays using C? Is
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
With dynamic allocation, using malloc:
This allocates an 2D array of size
dimension1_max*dimension2_max. So, for example, if you want a 640*480 array (f.e. pixels of an image), usedimension1_max= 640,dimension2_max= 480. You can then access the array usingx[d1][d2]whered1= 0..639,d2= 0..479.But a search on SO or Google also reveals other possibilities, for example in this SO question
Note that your array won’t allocate a contiguous region of memory (640*480 bytes) in that case which could give problems with functions that assume this. So to get the array satisfy the condition, replace the malloc block above with this: