1

So yeah, this is the error and I'm not sure how to fix this: test.c:5:12: error: array has incomplete element type 'bool []'

This is part of a bigger code but all that's relevant is included.

#include <stdio.h>
#include <cs50.h>

int candidate_count = 3;
bool locked[][] = {locked[0][1] = false, locked[0][2] = false, locked[1][0] = false, locked[1][2] = false, locked[2][0] = false, locked[2][1] = false};


int main(void)
{
    int lockedCount = 0;
    for(int i = 0; i < candidate_count; i++)
        for(int j = 0; j < candidate_count; j++) {
            if(locked[i][j] == false) {
                locked[i][j] = true;
                printf("locked %i vs %i\n", i, j);
            }
            if(i == candidate_count - 2) {
                for(int k = 0; k < candidate_count; k++)
                    if(locked[k][j] == true) {
                        lockedCount += 1;
                        printf("locked %i vs %i\n", i, k);
                    }
                if(lockedCount == 0) {
                    printf("didn't %i vs %i\n", i, j);
                    break;
                }
                else {
                    locked[i][j] = true;
                    printf("locked %i vs %i\n", i, j);
                }
            }
        }
}
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185

1 Answers1

5

These declaration and initialization of an array

bool locked[][] = {locked[0][1] = false, locked[0][2] = false, locked[1][0] = false, locked[1][2] = false, locked[2][0] = false, locked[2][1] = false};

is incorrect at least because the element of the array type bool[] is an incomplete type.

You need to write for example like

bool locked[][3] = 
{
    { [1] = false, [2] = false },
    { [0] = false, [2] = false }, 
    { [0] = false, [1] = false }
};

Or alternatively like

bool locked[][3] = 
{
    [0] = { [1] = false, [2] = false },
    [1] = { [0] = false, [2] = false }, 
    [2] = { [0] = false, [1] = false }
};

In fact these declaration and initialization is equivalent to

bool locked[3][3] = 
{
    false
};

because all elements of the array in your declaration are initialized by zeroes that is the value of the macro false..

Or as the array has static storage duration then it is by default initialized by zeroes. So you could even write

bool locked[3][3];

And you forgot to include the header <stdbool.h>. It is better to include explicitly headers that are required.

Here is a demonstrative program.

#include <stdio.h>
#include <stdbool.h>

bool locked[][3] = 
{
    { [1] = false, [2] = false },
    { [0] = false, [2] = false }, 
    { [0] = false, [1] = false }
};

int main(void) 
{
    // your code goes here
    return 0;
}
Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
  • Thank you for the solution! But could you explain it to me like I was five, what exactly "incomplete type" means in this case? – ILoveYeezis Mar 02 '21 at 09:04
  • 1
    @ILoveYeezis The C compiler can't deduce the size of "multi-dimensional" array, only the first "dimension" can be omitted when declaring arrays of arrays. – Some programmer dude Mar 02 '21 at 09:06
  • 1
    @ILoveYeezis You declared a two-dimensional array. The element type of your two-dimensional array is bool[]. That is the size of an object of the type bool[] is unknown. if you will try for example to write printf( "%zu\n", sizeof( bool[] ) ); the compiler will issue a similar error. – Vlad from Moscow Mar 02 '21 at 09:07
  • 1
    As a global variable `locked` gets initialized with `0`s, therefore the whole initialization list is unnecessary. `bool locked[3][3];` does the same thing, all entries are `false`. – mch Mar 02 '21 at 09:16
  • So with one dimensional array the program has no problem of not knowing the size of object? – ILoveYeezis Mar 02 '21 at 09:22
  • @ILoveYeezis When you have a one-dimensional array when the element type is bool the size of which (that is the type _Bool) is known. When you have a two-dimensional array then the element type is bool[] that is a one-dimensional array of unknown size. So the compiler issues an error. – Vlad from Moscow Mar 02 '21 at 09:23
  • Alright, last question - in your example you declared the boolean statements for each dimension separately. How would I define that locked[1][0] is true? Like this: { [1] = true, [0] = true }? A bit confusing notation, as in I'm not sure why I'm declaring them separately if they're tied. @VladfromMoscow – ILoveYeezis Mar 02 '21 at 09:30
  • 1
    @ILoveYeezis All approaches are shown in my answer. You could write for example [1] = { [0] = true } – Vlad from Moscow Mar 02 '21 at 09:34