Solved

Multidimensional Array Dynamically Allocated

Posted on 1998-09-10
4
255 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 20 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now