Solved

Multidimensional Array Dynamically Allocated

Posted on 1998-09-10
4
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 conditional statements in the C programming language.

751 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