2

I need to initialize a h x h matrix in C with spaces.

How to properly do it without cycles?

int h = 8;
char arr[h][h] = {{' '}}; // does not work.... 
8
  • 1
    You can't have partial initializer here. Will have to provide a full initializer. Partial initializers are assuming zeros for the parts which are missing. Commented Dec 10, 2020 at 18:17
  • This is a variable-length-array (VLA), where the dimension is variable. You can't initialize VLAs. If you make h constant (e.g. an enumerated value), you can initialize it. Commented Dec 10, 2020 at 18:18
  • @MOehm, if constant, how to do it? Commented Dec 10, 2020 at 18:18
  • 3
    Have you tried memset()? Commented Dec 10, 2020 at 18:18
  • Or this? Commented Dec 10, 2020 at 18:19

2 Answers 2

1

These declarations

int h = 8;
char arr[h][h] = {{' '}};

declare a variable length array. Variable length arrays may be declared only within functions (as for example within main) because they shall have automatic storage duration and may not be initialized in declarations.

So you can write for example

#include <string.h>

//...

int main( void )
{
    int h = 8;
    char arr[h][h];

    memset( arr, ' ', h * h );
    //...
}

That is you can apply the standard function memset that sets all characters of the array with the space character ' '.

Even if you have a non-variable length array nevertheless to initialize its all elements with the space character it is better to use the function memset.

#include <string.h>

//...

int main( void )
{
    enum { h = 8 };
    char arr[h][h];

    memset( arr, ' ', h * h );
    //...
}
Sign up to request clarification or add additional context in comments.

1 Comment

memset(arr, ' ', sizeof arr) should work as well
0

From GNU site:

To initialize a range of elements to the same value, write ‘[first ... last] = value’. This is a GNU extension

You can use designated initializers. But this type of initialization works, only for constant number of rows and columns.

char arr[8][8] = { { [0 ... 7] = ' '}, { [0 ... 7] = ' '}, { [0 ... 7] = ' '}, { [0 ... 7] = ' '}, { [0 ... 7] = ' '}, { [0 ... 7] = ' '}, { [0 ... 7] = ' '}, { [0 ... 7] = ' '}  };

11 Comments

This is not a standard C feature. Moreover it uses the magic number 7.
Yes, designated initializers are fine... but they don't get around the fact that you're initializing a VLA. If you made the dimensions constants, it would work.. but as is?
It's a VLA because the dimensions are specified with a variable rather than a constant.
What you really want to do is: { [0 ... h] = ' '}, but I'm not sure you can because h is int h = 8;. Even if we do: #define h 8, there's an issue. If we change the value (e.g. to 9), the sequence breaks [not enough repetition]. Designated initializers are fine [I use them a lot], but doing memset or some for loops is actually a better solution for this given problem.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.