5

I am trying to build two dimensional array by dynamically allocating. My question is that is it possible that its first dimension would take 100 values, then second dimension would take variable amount of values depending on my problem? If it is possible then how I would access it? How would I know the second dimension's boundary?

why
1,6473 gold badges22 silver badges49 bronze badges
asked Nov 12, 2013 at 3:41
1
  • 2
    This totally depends on how you implement the 2D array. Is it an array-of-arrays (e.g. int matrix[M][N]) or is it a standard array, that you're applying two "logical" dimensions to (e.g. int matrix[M*N])? Commented Nov 12, 2013 at 3:44

4 Answers 4

5

(See the comments in the code)

As a result you'll get an array such like the following:

enter image description here

// Create an array that will contain required variables of the required values
// which will help you to make each row of it's own lenght.
arrOfLengthOfRows[NUMBER_OF_ROWS] = {value_1, value_2, ..., value_theLast};
int **array;
array = malloc(N * sizeof(int *)); // `N` is the number of rows, as on the pic.
/*
if(array == NULL) {
 printf("There is not enough memory.\n");
 exit (EXIT_FAILURE);
}
*/
// Here we make each row of it's own, individual length.
for(i = 0; i < N; i++) {
 array[i] = malloc(arrOfLengthOfRows[i] * sizeof(int)); 
/*
if(array[i] == NULL) { 
 printf("There is not enough memory.\n");
 exit (EXIT_FAILURE); 
}
*/
}
answered Nov 12, 2013 at 3:51
Sign up to request clarification or add additional context in comments.

8 Comments

This ignores the question's request for dynamically sized inner arrays.
@Martin, What do you mean exactly? (If I understood the question incorrectly, I will delete this answer)
<pedenatic>Your code does not compile as-is. I would either remove the #include, or wrap the rest of the code up in a function. Including the #include line somewhat implies that this is a compilable example.
From the question: "then second dimension would take variable amount of values depending on my problem". You're allocating a fixed number of elements.
Nice work. +1 for the image, and the idea of tracking lengths in a separate array.
|
2

You can use array of 100 pointers:

int *arr[100];

then you can dynamically allocate memory to each of the 100 pointers separately of any size you want, however you have to remember how much memory (for each pointer) you have allocated, you cannot expect C compiler to remember it or tell it to you, i.e. sizeof will not work here.

To access any (allowed, within boundary) location you can simply use 2D array notation e.g. to access 5th location of memory allocated to 20th pointer you can use arr[20][5] or *(arr[20] + 5).

answered Nov 12, 2013 at 3:51

1 Comment

To keep track of the number of items in each inner array, either wrap the arrays in a struct that stores the size, or change it to int **arr[100] and use "null termination" on the values.
1

I believe the OP wants a single chunk of memory for the array, and is willing to fix one of the dimensions to get it. I frequently like to do this when coding in C as well.

We all used to be able to do double x[4][]; and the compiler would know what to do. But someone has apparently messed that up - maybe even for a good reason.

The following however still works and allows us to use large chunks of memory instead of having to do a lot of pointer management.

#include <stdio.h>
#include <stdlib.h>
// double x[4][];
struct foo {
 double y[4];
} * x;
void
main(int ac, char * av[])
{
 double * dp;
 int max_x = 10;
 int i;
 x = calloc(max_x, sizeof(struct foo));
 x[0].y[0] = 0.23;
 x[0].y[1] = 0.45;
 x[9].y[0] = 1.23;
 x[9].y[1] = 1.45;
 dp = x[9].y;
 for (i = 0; i < 4; i++)
 if (dp[i] > 0)
 printf("%f\n", dp[i]);
}

The trick is to declare the fixed dimension in a struct. But keep in mind that the "first" dimension is the dynamic dimension and the "second" one is fixed. And this is the opposite of the old way ...

You will have to track the size of your dynamic dimension on your own - sizeof can't help you with that.

Using anonymous thingies you might even be able to git rid of 'y'.

answered Oct 19, 2015 at 20:58

Comments

1

Using a single pointer:

int *arr = (int *)malloc(r * c * sizeof(int));

/* how to access array elements */

for (i = 0; i < r; i++)
 for (j = 0; j < c; j++)
 *(arr + i*c + j) = ++count; //count initialized as, int count=0;

Using pointer to a pointer:

int **arr = (int **)malloc(r * sizeof(int *));
for (i=0; i<r; i++)
 arr[i] = (int *)malloc(c * sizeof(int));

In this case you can access array elements same as you access statically allocated array.

answered Aug 23, 2017 at 2:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.