2

I have this global enum and a 3D array:

enum place { SCISSORS, DRILL, BENDING_MACHINE, WELDER, PAINT_SHOP, SCREWDRIVER, MILLING_CUTTER };

const int placeRecipeIndexes[_PLACE_COUNT][_PHASE_COUNT][TOOLS_REPEAT_COUNT] = {
        [SCISSORS] = {{0, EMPTY}, {1, EMPTY}, {EMPTY, EMPTY}},
        [DRILL] = {{1, 4}, {0, 3}, {1, 3}},
        [BENDING_MACHINE] = {{2, EMPTY}, {EMPTY, EMPTY}, {EMPTY, EMPTY}},
        [WELDER] = {{3, EMPTY}, {EMPTY, EMPTY}, {EMPTY, EMPTY}},
        [PAINT_SHOP] = {{5, EMPTY}, {4, EMPTY}, {5, EMPTY}},
        [SCREWDRIVER] = {{EMPTY, EMPTY}, {5, EMPTY}, {2, EMPTY}},
        [MILLING_CUTTER] = {{EMPTY, EMPTY}, {2, EMPTY}, {0, 4}}
};

and I need a pointer (or possibly a copy) which points to a particular 2D sub-array of placeRecipeIndexes which means that by pointing to placeRecipeIndexes[0], I would have a 2D array looking like this:

{{0, EMPTY}, {1, EMPTY}, {EMPTY, EMPTY}}.

At first, I tried it without a pointer: const int indexes[_PHASE_COUNT][TOOLS_REPEAT_COUNT] = toolsIndexes[idx]; but it gave me:

Array initializer must be an initializer list.

so I tried to do it like this:

const int **indexes = (const int **) toolsIndexes[idx];

but I can't access the indexes array positions because they're presumably empty - I'm getting SIGSEV.

I thought that this should definitely work. Am I missing something important here?

MRE:

#include <stdio.h>

#define EMPTY -1

enum place { SCISSORS, DRILL, BENDING_MACHINE, WELDER, PAINT_SHOP, SCREWDRIVER, MILLING_CUTTER };

const int placeRecipeIndexes[7][3][2] = {
        [SCISSORS] = {{0, EMPTY}, {1, EMPTY}, {EMPTY, EMPTY}},
        [DRILL] = {{1, 4}, {0, 3}, {1, 3}},
        [BENDING_MACHINE] = {{2, EMPTY}, {EMPTY, EMPTY}, {EMPTY, EMPTY}},
        [WELDER] = {{3, EMPTY}, {EMPTY, EMPTY}, {EMPTY, EMPTY}},
        [PAINT_SHOP] = {{5, EMPTY}, {4, EMPTY}, {5, EMPTY}},
        [SCREWDRIVER] = {{EMPTY, EMPTY}, {5, EMPTY}, {2, EMPTY}},
        [MILLING_CUTTER] = {{EMPTY, EMPTY}, {2, EMPTY}, {0, 4}}
};

int main() {
    const int **indexes = (const int **) placeRecipeIndexes[0];

    printf("{");
    for (int i = 0; i < 3; i++) {
        printf("{%d, ", indexes[i][0]);
        if (i != 2) {
            printf("%d}, ", indexes[i][1]);
        }
        else {
            printf("%d}", indexes[i][1]);
        }
    }
    printf("}\n");

    // The output should be: {{0, -1}, {1, -1}, {-1, -1}}
    return 0;
}
3
  • Your question is very confusing. Please post a minimal reproducible example so we can reproduce the problem and help you debug it. Commented Nov 8, 2020 at 0:49
  • @user3629249 Added. Commented Nov 8, 2020 at 1:02
  • @tomashauser (const int **) placeRecipeIndexes[0]; This cast is wrong because the type of placeRecipeIndexes[0] is int[3][2] which can decay to int (*)[2] but not int **. Commented Nov 8, 2020 at 1:14

1 Answer 1

1

Replace:

    const int **indexes = (const int **) placeRecipeIndexes[0];

with:

    const int (*indexes)[2] = placeRecipeIndexes[0];     // C

or:

    const int (&indexes)[3][2] = placeRecipeIndexes[0];  // C++

For verification, the following check out (in C++):

static_assert(&indexes[0][0] == &placeRecipeIndexes[0][0][0]);
static_assert(&indexes[2][1] == &placeRecipeIndexes[0][2][1]);
Sign up to request clarification or add additional context in comments.

5 Comments

I'm getting error: expected identifier or ‘(’ before ‘&’ token.
@tomashauser It's compiling fine here.
Well I'm using -std=c99 - it may be that.
@tomashauser Updated the answer for C, verified here.
Great, it works now. Funny thing is that I was trying this but without the brackets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.