tags:

views:

148

answers:

3

In C a programmer can declare an array like so:

unsigned char Fonts[2][8] {
    [1] = {0, 31, 0, 31, 0, 31, 0, 31}
};

And element [0] is likely random bits. Is there a similar way in C++?

+1  A: 

Why not just do

unsigned char Fonts[2][8] {
    {0, 0, 0, 0, 0, 0, 0, 0},
    {0, 31, 0, 31, 0, 31, 0, 31}
};

?

John at CashCommons
Maybe its a very sparse array?
Jherico
Because I'm borrowing C code that's already structured similar to the example I gave. I'm hoping for a simple solution that doesn't require me doing this by hand. :)
Scott
For a small 2 and 8 it might work, imagine, Fonts[62][72]
Murali VP
Why not keep the C code and make you program a combination of C and C++?
Richard Pennington
Richard, well I didn't have to do it by hand after all, so no huge deal. Small C program did it for me.
Scott
+5  A: 

You can do this:

unsigned char Fonts[2][8] = {
    {0},
    {0, 31, 0, 31, 0, 31, 0, 31}
};
Tim Sylvester
This zeroes out all Fonts[0][...]?
John at CashCommons
As indicated by BjoernD you can omit the single "0", but leaving it in keeps the code C89-compatible.
Tim Sylvester
@John W Yes, as long as any initializer is specified, any values not given are set to zero.
Tim Sylvester
+6  A: 

This works in C++:

unsigned char foo [2][8] = {
        {},
        {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
};

Here, foo[0] is zero-initialized as defined by the C++ standard (§8.5.5 - default initialization for POD types is zero-initialization). For non-POD-types the default constructor is called.

BjoernD
So this leaves foo[0][...] undefined?
John at CashCommons
No. foo[0] is zero-initialized as defined by the C++ standard (§8.5.5 - default initialization for non-POD types is zero-initialization).
BjoernD
Sorry, I meant default initialization for POD types. For non-POD, of course, the default constructor is called.
BjoernD
"default initialization for POD typs is zero-initialization". It's actually value-initialization that delegates to zero-initialization here, not default initialization. (your statement is true for '98, but not for '03 revision of C++)
Johannes Schaub - litb