I want to initialize an int array with constant values, with all the 2^n values from 2^0 to 2^31 and I want to know which method below is right and if it gives the result I want and if there are easier or shorter methods to initialize it.
static const char two_n[32];
two_n[32] = {1, 2 ,4 ,8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096,
8192, 16384, 32768, 65535, 131070, 262140, 524280, 1048560,
2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728,
268435456, 536870912, 1073741824, 2147483648};
or
static const char *two_n[32];
two_n[32] = {1, 2 ,4 ,8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096,
8192, 16384, 32768, 65535, 131070, 262140, 524280, 1048560,
2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728,
268435456, 536870912, 1073741824, 2147483648};
or
static const int two_n[32];
two_n[32] = {1, 2 ,4 ,8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096,
8192, 16384, 32768, 65535, 131070, 262140, 524280, 1048560,
2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728,
268435456, 536870912, 1073741824, 2147483648};
forloop when the program starts. Or you could use hexadecimal notation for the constants, since the hexadecimal values follow a nice obvious pattern.constarray is declared, it cannot be assigned to. (otherwise it would not beconst.)