Solved

dynamically allocate 2 dimensional array,

Posted on 1998-11-10
2
336 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
Comment Utility
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
Comment Utility
thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

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…
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 how to create, access, and change arrays in the C programming language.

743 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