Hi, as you see from the title I'm wondering is it possible to initialize global variable like
Code:
static const int daytab[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
except that I'm trying to make a pointer version so I tried to initialize
Code:
static int** digits = /*...*/
It seems to not work even with creating function initialize() and even if I remove the static keyword
Code:
int** initialize()
{
int** p = (int**)malloc(sizeof(int**) * 2);
for (int i = 0; i < 2; ++i)
*(p + i) = (int*)malloc(sizeof(int) * 12);
/*initialize elements*/
return p;
}
int** digits = initialize();
I'm getting the error that this function call is not allowed in a constant expression.
It was an exercise to do some stuff without using [] so I'm trying to do this. Is this even possible to do? What would be the closest thing to do this if some [] have to be used like
Code:
static int* digits[2] = /*...*/
And even if I would succeed there would be no way to release the resources allocate by malloc when the program terminates. This is probably why its not allowed right?