Computing.Net > Forums > Programming > C 2-D array

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C 2-D array

Reply to Message Icon

Name: Selma Li
Date: October 2, 2003 at 07:09:53 Pacific
OS: Window 2000
CPU/Ram: 256K
Comment:

Hi,

How do you dimension a 2-D array in C? I know in C++, I can use dynamic allocation (pointer to pointer). In C, I read about malloc. How does malloc works and what is the format?

My array size is determine during compiling (through calculations done). But the array is define with const int always. How can I get over that?

Also, how do I free my menory?

Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: AlexStocko
Date: October 3, 2003 at 13:22:01 Pacific
Reply:

Straight forward multi-dimensional arrays are not possible with malloc because, for example with a hard-coded 2 dimensional array, the compiler knows the number of columns in each row and can therefore perform the required calculations to access an element. So for a 3 by 2 array:

int iMArray[3][2];
iMArray[2][1] = 20;
the compiler would access the element by transforming the statement into something like this:

*(iMArray + (2 * (3 * sizeof(int))) + (1 * sizeof(int))) = 20;
where the (3 * sizeof(int)) is the size of a row. This value would not be calculable in the case of a malloc array.

It is possible to simulate a multi-dimensional array with malloc though, in order that it can be accessed in the same manner as that given above for the hard-coded array. To do this you have to create an array of pointers.

array of pointers:
Things become more complicated when it is necessary to represent an array of strings for example. This is because we are now considering either 2 dimensional arrays or, more commonly put, arrays of arrays.

The string case, an array of arrays of char is pretty complicated to explain, so let us consider a more simple example:

int *ipArray[10];
int iMArray[10][20];
iMArray[2][6] = 12;
ipArray[2][6] = 12;
Both the assignment statements are correct C code, but only the first one will succeed at this point. This is because the int * array only has enough memory reserved for 10 int pointers, and even these are unusable as they don't point to anything useful yet, whereas the hard-coded array has enough memory now for the desired 200 ints.

To make the statement work the pointers (the array rows) need to be filled in to point to the array column data before the assignment is performed. This can be a trivial for loop and malloc construct, inserted after the array definitions:

int *ipArray[10];
int iMArray[10][20];
int i;
for (i = 0; i < 10; i++) ipArray[i] = (int *)malloc(20 * sizeof(int));
iMArray[2][6] = 12;
ipArray[2][6] = 12;
This would then allocate the 10 rows of 20 ints required. More memory is used with a malloc based multi-dimensional array compared to a hard-coded one because of the intermediate pointers required, the 10 int *s in this case.

The same assignment statement can be used for the malloc array because the compiler again performs some statement transformations:

*(*(ipArray + (2 * sizeof(int *))) + (6 * sizeof(int))) = 12;
It can be seen that 2 memory accesses are required, the first one to get the address of the column data for the row, and the second to store the desired value in the calculated location.

Expanding the above example to handle an array of strings is not too bad once it is understood that the arrays have to constructed before they can be used, thus:

char cStrArray[10][20];
char *cpArray[10];
int i;
for (i = 0; i < 10; i++) cpArray[i] = (char *)malloc(20 * sizeof(char));
strcpy(cStrArray[5], "A hat is on the mat");
strcpy(cpArray[5], "A hat is on the mat");
cStrArray[5][3] = 'u';
cpArray[5][3] = 'u';
is perfectly okay, the character access to change the 'a' to a 'u' in the char * case would be performed something like this:

*(*(cpArray + (5 * sizeof(char *))) + (3 * sizeof(char))) = 'u';
very similar indeed to the int example.


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C 2-D array

Return 2-D Array in C www.computing.net/answers/programming/return-2d-array-in-c/13956.html

2-d array from a file in c++ www.computing.net/answers/programming/2d-array-from-a-file-in-c/3907.html

2-D Array Help C++ www.computing.net/answers/programming/2d-array-help-c/14084.html