简体   繁体   中英

create a pointer to a 2D array

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.

这应该工作:

void change_to_true(bool** array, int i, int j)

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.

This should answer you question.

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

[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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM