Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

dynamically allocate 2 dimensional array,

Posted on 1998-11-10
2
Medium Priority
?
381 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
[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
2 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

604 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