Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Multidimensional Array Dynamically Allocated

Posted on 1998-09-10
4
Medium Priority
?
296 Views
Last Modified: 2010-04-15
I am wondering how to implement a multidimensional array based on a users input.

Basically the user will enter data that will determine how many rows there are and I have a fixed number of columns.  How can I dynamically allocate this to work?

Thanks
Darrell
0
Comment
Question by:larockd
  • 3
4 Comments
 
LVL 1

Accepted Solution

by:
Bonev earned 80 total points
ID: 1252637
Suppose COLUMNS is the fixed number of columns.
Rows is user input number of rows.

/* allocation */
int* pArray;
pArray = (int*)malloc(sizeof(int)*COLUMNS*Rows);

/* access to row i and column j */
int item;
item = pArray[COLUMNS*i+j];

0
 
LVL 1

Expert Comment

by:Bonev
ID: 1252638
To release the memory use free(pArray);
0
 

Author Comment

by:larockd
ID: 1252639
Iwas able to use your code and get it to work.  I do have some questions on the theory behind this.

It would appear as if this is a single dimensional array.  I say this because all the memory is accesses sequentially, but isnt that a property of a multidimensional array.  

I was wondering if you could break down the malloc statement for me..


    pArray = (int*)malloc(sizeof(int)*COLUMNS*Rows);

I am allocating the size of int * Columns * rows which allocates Columns * rows of integers that pArray points to.  Why did you type cast malloc as (int *) is this because it is a pointer to begin with?


Thanks


0
 
LVL 1

Expert Comment

by:Bonev
ID: 1252640
Well, the multidimensional arrays are just an abstraction, because the computer memory is linear (one dimension). The programming languages provide means to work with MD arrays, but in fact their representation is linear. Since you don't know the size of the dimensions, you should simulate what the compiler does for you.
You're correct about the malloc statement.
Your 2D array actually is a sequence of Rows single dimensional arrays of size COLUMNS. If you want to access row i and column j, you need first to reach the first item of i-th 1D array using COLUMNS*i, and then you have to point to the j-th item in that array, adding j.


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question