-1

I am attempting to write a C program which reads input from a text file and puts it into a 2D-array where Rows are lines and columns are characters.

However I am confused after reading the following article:

http://c-faq.com/aryptr/pass2dary.html

The function definition which I am thinking of is

int processArray(char **text) {
    ...
    ...
}

where I am attempting to pass in a pointer to a 2D array whose dimensions I don't know until runtime. I would like to be able to access the elements using two square brackets [] []

However in the link it says the following:

An intermediate pointer would have to be used when attempting to call it with a two-dimensional array:

extern g(int **ipp);
int *ip = &array[0][0];
g(&ip);       /* PROBABLY WRONG */

but this usage is misleading and almost certainly incorrect, since the array has been ``flattened'' (its shape has been lost).

  1. What is wrong with the above declaration?
  2. How should you define an array to multiple dimensions?
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Har
  • 3,727
  • 10
  • 41
  • 75
  • 1
    `int processArray(int r, int c, char text[r][c]);` .....`processArray(ROWS, COLS, matrix);` Take a look at [VLA](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html)s – LPs Mar 31 '17 at 13:09
  • Something like `int **` is **not** a 2D array and cannot be used as one! It is a completely different datatype. The text is indeed missleading and wrong as it stands. Note that K&R is outdated since 18 years. – too honest for this site Mar 31 '17 at 13:11
  • That is what I was fearing, how do you do this? Note: VLA is one option for this, how can I achieve it otherwise? Could you also explain why a int ** is not a 2D array? – Har Mar 31 '17 at 13:17
  • 1
    Because it just isn't. It's a pointer. It's size is fixed. It has no storage in itself. It is not an array. Pointers are not arrays. Arrays of pointers can point to sparse data with gaps. That is because such data is not in an array. An array has no gaps, has storage and is not a pointer. – ThingyWotsit Mar 31 '17 at 13:22
  • Okay I understand, I would like to pass a pointer to an array of arbitrary size so that I do not have to copy the entire array onto the stack and have the ability to index it using two [] [] – Har Mar 31 '17 at 13:27
  • Possible duplicate: [Correctly allocating multi-dimensional arrays](http://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Mar 31 '17 at 13:28
  • you ca use a simple pointer and then `*(pointer+selected_col+(selected_row*MAX_COL))` – LPs Mar 31 '17 at 13:43
  • 1
    2D arrays in C and C++ soon casue trouble. Your best bet is to flatten and pass everything around as a 1D array with width, height and if necessary depth. – Malcolm McLean Mar 31 '17 at 13:45
  • 1
    Since we're discussing storing text, which typically has lines of varying length, wouldn't a 2D array of char be rather wasteful compared to the `char **` form? – Ian Abbott Mar 31 '17 at 13:53
  • Yes I agree so maybe a 2D array is not what I need. Also as indicated above, probably a 1D array of char * (strings) – Har Mar 31 '17 at 14:11

1 Answers1

0

Leaving aside VLAs you can use a simple pointer to point your matrix:

#include "stdio.h"

#define MAX_ROWS 2
#define MAX_COLS 2

void test (int *matrix, int col_max, int row_max)
{
    for (int i=0; i<row_max; i++)
    {
        for (int j=0; j<col_max; j++)
        {
            printf("matrix[%d][%d]= %d\n", i, j, *(matrix+(i*col_max)+j) );
        }
    }

}
int main(void)
{
    int matrix[MAX_ROWS][MAX_COLS] = { {1,2}, {3,4} };

    test(&matrix[0][0], MAX_ROWS, MAX_COLS);

    return 0;
}
LPs
  • 16,045
  • 8
  • 30
  • 61