Solved

dynamically allocate 2 dimensional array,

Posted on 1998-11-10
2
367 Views
Last Modified: 2010-04-15
I would like to dynamically allocate memory for a two dimensional array, and use array[x][y] notation on it, how can this be done?
0
Comment
Question by:mayobh
2 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
ID: 1254158
Since the dimensions are not known the compiler cannot generate appropriate address arithmetic for a two dimensional array at compile time. A pointer into a block of memory can't be used to offset into the nth row, if you don't know how long each row is. However, the compiler can generate address arithmetic for a one dimensional array. All it needs is the size of the elements. For integers for instance that would be 4 bytes (in most circumstances today), so given a pointer, the nth element is n*4 bytes from the beginning.

So, the way to get a two dimensional array, is to organize it based on an array of pointers. Declare:

int **matrix;

Now allocate the "row pointers" (assume the variable x contains the number you need):

matrix=(int **)calloc(x,sizeof(int *));

Now allocate space for each row:

for(i=0; i<x; ++i)matrix[i]=(int *)calloc(y,sizeof(int));

Now you have a variable named matrix, which points to an array of pointers. Each element in the array points to an array of integers. And you get to use it with the familiar syntax:

a=matrix[i][j];

The "invisible" difference is that a double dereference is taking place. Instead of address calculating its way into a block of memory, it follows the pointer from matrix to an array of integer pointers. Finds the right row, the follows that pointer to an array of integers and finds the right element.


0
 

Author Comment

by:mayobh
ID: 1254159
thanks
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…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
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.

713 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