0

Is it possible in C to make an empty 2D array with known number of rows and columns in the main.

e.g. int my_array[10][10]

then pass it in a function (empty):

my_function(r,c,my_array) in order to perform some operations and full the 2D array and return it full in the main?

If yes: I have to malloc-ed? dynamically right? and "free" in main? I use void or return my_array? I have found some examples in the internet and I have been confused. Is there any example to have as guide?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Please show the whole but minimal code that you have tried. There is no such thing as an "empty" or "full" array: every element has *some* value whether or not you explicitly assign values. – Weather Vane Sep 29 '16 at 19:24
  • You will find the answer by minimal search effort and in a good C book. The latter might require understanding fundamental concepts of C, though... – too honest for this site Sep 29 '16 at 19:25
  • Possible duplicate of [Pass 2D array to function by reference](http://stackoverflow.com/questions/11862054/pass-2d-array-to-function-by-reference) – tinman Sep 29 '16 at 19:26
  • @tinman: C does not support references and that question is about a jagged array which is not and cannot be a 2D array. But to me OP is confused at multiple levels. – too honest for this site Sep 29 '16 at 19:28
  • @WeatherVane I am sorry I mean that I create for example: a table of 10 rows and 10 columns type of int but I don't have assign yet values. e.g I only defined the table int my_array[10][10]; not assign values. – Giannis Kalamaras Sep 29 '16 at 19:34
  • @Olaf: I do not see why the Q I linked to is about jagged arrays and the poster in that Q wants to pass by reference, which is a suitable name for not passing by value. No mention of references in the c++ sense. – tinman Sep 29 '16 at 19:38
  • @tinman: You cannot other than pass by value in C! A pointer is a first-class object. Major problem is something like `int **` **cannot** point to a 2D array! – too honest for this site Sep 29 '16 at 19:41

1 Answers1

1

When you define variable like this:

int my_array[10][10];

It creates a 2D array of integers. There is nothing to malloc or free when you do this.

You would declare myfunction as follows:

void myfunction(int r, int c, int array[r][c])

Then inside of this function you can modify array and the the changes would be viewable in the calling function.

Note that this syntax uses a variable sized array parameter based on the other two parameters.

EDIT:

Here's an example of how you could use this:

void initialize(int r, int c, int array[r][c])
{
    int i,j;
    for (i=0;i<r;i++) {
        for (j=0;j<c;j++) {
            array[i][j] = 0;
        }
    }
}

int main()
{
    int my_array[10][10];

    initialize(10,10,my_array);

    // my_array now contains all zeros
    ...
    return 0;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Just another one: as we already pass both dimensions (which I support), it might be easier to use array notiation with both dimensions give. I'm aware this is mostly a matter of style, but it documents the intention better imo. – too honest for this site Sep 29 '16 at 19:30
  • @dbush the definition is inside the main. I call a function inside the main and pass as argument the table to a function in order to perform some operations.. any help? – Giannis Kalamaras Sep 29 '16 at 19:36
  • @GiannisKalamaras: Yes, and? What's the problem? No offence, but you seem to missed some required prerequisites of C programming. – too honest for this site Sep 29 '16 at 19:39