Solved

Multidimensional Array Dynamically Allocated

Posted on 1998-09-10
4
242 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
Comment Utility
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
Comment Utility
To release the memory use free(pArray);
0
 

Author Comment

by:larockd
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to use strings and some functions related to them 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.

762 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

12 Experts available now in Live!

Get 1:1 Help Now