0

I have a bool array[size][size] and want to pass a pointer of it to a function to change its value.

void change_to_true(???, int i, int j) { // don't know what type it should be
  array[i][j] = 1;
}

How do I create a pointer of the array, and how should I pass the pointer to the function so that I can change its value? BTW it's c code.

Thanks

Edit: I tried

static bool array[size][size];
bool (*array_t)[index] = array;
void change_to_true(bool array, int i, int j);

Compiler told me subscripted value is neither array nor pointer I tried to change the type definition of the function parameter from bool array to bool* array, still not work.

I also tried bool (*array_t)[][size] = &array and bool (*array_t)[size][size] = &array; for the pointer, still not work.

Also since the size of the array is decided during the runtime, I can't have a enum or a global array defined that way.

user1537085
  • 404
  • 5
  • 18

4 Answers4

1

This should work:

void change_to_true(bool** array, int i, int j)
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Actually, it's a pointer to a pointer to one or more `bool`s. Arrays devolve to pointers when used as function arguments. – Jonathan Grynspan Feb 03 '13 at 01:18
  • @JonathanGrynspan I couldn't remember which it was. Maybe my first instinct was correct...I'll change it back. – Code-Apprentice Feb 03 '13 at 01:19
  • No. Neither `(bool *array[], ...)` nor `(bool **array, ...)` is correct. – Jonathan Leffler Feb 03 '13 at 01:21
  • I'm not saying it's correct, I'm saying that's what `bool* array[]` would be if used as a function argument. – Jonathan Grynspan Feb 03 '13 at 01:23
  • I've removed my original comment; it wasn't accurate enough. My second comment stands as accurate, I believe. Produce compiling, running code — by all means crib anything you want from my answer while doing so — to come up with a version that works with `bool *array[]` or `bool **array`... – Jonathan Leffler Feb 03 '13 at 01:35
1

Simple solution — but no pointer to 2D array

The function should be simply:

#include <stdbool.h>

enum { size = 24 };

extern void change_to_true(bool array[][size], int i, int j);

void change_to_true(bool array[][size], int i, int j)
{
    array[i][j] = true;
}

int main(void)
{
    bool array[size][size];

    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
             array[i][j] = false;
             change_to_true(array, i, j);
        }
    }
}

I'm assuming, of course, that you have a C99 or C2011 compiler. If you're stuck with C89, then you'll have to provide definitions for bool and true; the rest does not need to change. I'm not convinced the function call is worth it, compared with the assignment of false.

Solution using pointer to 2D array

Note that the code above does not, strictly, create a pointer to a 2D array. If you are really, really sure that's what you want, then the notation changes:

#include <stdbool.h>

enum { size = 24 };

extern void change_to_true(bool (*array)[][size], int i, int j);

void change_to_true(bool (*array)[][size], int i, int j)
{
    (*array)[i][j] = true;
}

int main(void)
{
    bool array[size][size];

    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
             array[i][j] = false;
             change_to_true(&array, i, j);
        }
    }
}

There's no benefit to using a formal 'pointer to array' that I can think of, though — not in this context.

Solution using C99 variable length arrays

In C99, you can also use a VLA — variable length array — like this:

#include <stdbool.h>

extern void change_to_true(int size, bool array[][size], int i, int j);

void change_to_true(int size, bool array[][size], int i, int j)
{
    array[i][j] = true;
}

int main(void)
{
    for (int size = 2; size < 10; size++)
    {
        bool array[size][size];

        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                array[i][j] = false;
                change_to_true(size, array, i, j);
            }
        }
    }
}

Note that the size must precede the array declaration in the function declaration. And yes, you also write this with pointer to array notation (actually, I edited the pointer to array notation code first, then realized that I wanted the simpler version).


Which to use?

Unless you have compelling reasons not mentioned in the question, the first version is the code to use.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

This should answer you question.

In short, you make a pointer to the first element and then pass it around.

Community
  • 1
  • 1
Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
0

[EDIT] C supports bool since C 99 Besides that, by declaring it that way, the actual array is a pointer to a memory zone with all the size^2 instances. This means that you can pass array to change it in the function, and in your header declare that you are receiving bool array[][]. That way it should work.

Afonso Tsukamoto
  • 1,184
  • 1
  • 12
  • 21
  • Where have you been this last 13+ years? C99 introduced `bool`. (Or, maybe, you should be asking your compiler vendor where it has been for the last 13+ years, if your compiler vendor hasn't caught up to that fact that it is now the 21st Century and no longer 1989.) – Jonathan Leffler Feb 03 '13 at 01:02
  • I actually learned that C didn't have bool, and every time I needed it I declare it with enum. Looks like I really am 14 years behind. Thanks! – Afonso Tsukamoto Feb 03 '13 at 01:20