2

Three questions in 1.

  1. If I have a 2-D array -

    int array_name[num_rows][num_columns] 
    

    So it consists of num_rows arrays -each of which is an array of size = num_columns. Its equivalent representation using an array of pointers is-

    int* array_name[num_rows] 
    

    -so the index given by [num_rows] still shows the number of 1-D arrays - somewhere using malloc we can then specify the size of each of the 1-D arrays as num_columns. Is that right? I saw some texts telling

    int* array_name[num_columns] 
    

    will the indices not get switched in this case ?

  2. For a ID array I specify size dynamically as-

    int *p;
    p = (int*) malloc (size * sizeof(int))
    

    For 2 D arrays do I specify the size of entire 2-D array or of one 1-D array in malloc -

    int*p [row_count];
    p = (int*) malloc (row_count * column_count * sizeof(int))
    

    or

    p = (int*) malloc (column_count * sizeof(int))
    

    I think it should be the second since p is a pointer to a 1-D array and p+1 is a pointer to a 1-D array etc. Please clarify.

  3. For ques 2 - what if p was defined as - int **p; rather than int * p[row_count] How will the malloc be used then? I think it should be -

    p = (int*) malloc (row_count * column_count * sizeof(int))
    

Please correct, confirm, improve.

melpomene
  • 84,125
  • 8
  • 85
  • 148

2 Answers2

2

Declaring :

int *array_name[num_rows];

or :

int *array_name[num_columns];

is the same thing. Only the name changes, but your variable is still referring to rows because C is a row major so you should name it row.

Here is how to allocate a 2D array :

int (*p)[column] = malloc (sizeof(int[row][column]);

An int ** can be allocated whereas int [][] is a temporary array defined only in the scope of your function.

Don't forget that a semicolon is needed at the end of nearly every line.
You should read this page for a more complete explanation of the subject

Comte_Zero
  • 252
  • 3
  • 19
  • No, to allocate a 2D array you would allocate it using a _pointer_ not an array. In you example `p` is already an allocated array. To allocate it dynamically, use `int (*p)[column] = malloc (sizeof(int[row][column]);`. Alternatively, you could make a look-up table thing with a `int** p` but that's bad practice in this case. See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Sep 06 '18 at 11:37
  • @Lundin I see where I was wrong and believe to have made the necessary corrections as so misconception should not spread thanks for pointing out the errors. Please tell me if I am correct now. – Comte_Zero Sep 06 '18 at 12:52
1
  1. (and 2.)

    If I have a 2-D array

    int array_name[num_rows][num_columns];

    So it consists of num_rows arrays -each of which is an array of size = num_columns.

If both num_rows and num_columns are known at compile time, that line declares an array of num_rows arrays of num_columns ints, which, yes, is commonly referred as a 2D array of int.

Since C99 (and optionally in C11) you can use two variables unknown at compile time and end up declaring a Variable-Length Array, instead.

Its equivalent representation using an array of pointers is

int* array_name[num_rows];

So the index given by [num_rows] still shows the number of 1-D arrays - somewhere using malloc we can then specify the size of each of the 1-D arrays as num_columns. Is that right?

Technically, now array_name is declared as an array of num_rows pointers to int, not arrays. To "complete" the "2D array", one should traverse the array and allocate memory for each row. Note that the rows could have different sizes.

Using this form:

int (*array_name)[num_columns];
//  ^           ^ note the parenthesis 
array_name = malloc(num_rows * sizeof *array_name);

Here, array_name is declared as a pointer to an array of num_columns ints and then the desired number of rows is allocated.

3.

what if p was defined as int **p;

The other answers show how to allocate memory in this case, but while it is widely used, it isn't always the best solution. See e.g.:

Correctly allocating multi-dimensional arrays

Bob__
  • 12,361
  • 3
  • 28
  • 42
  • Note that at least the two most-upvoted answers posted to [How do we allocate a 2-D array using One malloc statement](https://stackoverflow.com/questions/8740195/how-do-we-allocate-a-2-d-array-using-one-malloc-statement) are not guaranteed to work because they don't guarantee suitable alignment of the data elements. This is much better: https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays – Andrew Henle Sep 06 '18 at 12:58
  • @AndrewHenle Good point, I should search for a better dupe. I liked the answers by [chux](https://stackoverflow.com/a/48909970/4944425) and [John Bode](https://stackoverflow.com/a/8742251/4944425), though. – Bob__ Sep 06 '18 at 13:08