S
Superfox il Volpone
I read in the glibc documentation that using 'static' in a function can
save the content of a variable.
So I want to use it to write a function that return the number of days
in one month. Since the days on one month are even the some, it could
be better to compute the array only one time. And so I did :
int nDaysOnMonth(int month, int year){
static int i_dmonths[12];
i_dmonths[0] = i_dmonths[2] = i_dmonths[4] = i_dmonths[6] =
i_dmonths[7] = i_dmonths[9] = i_dmonths[11] = 31;
i_dmonths[3] = i_dmonths[5] = i_dmonths[8] = i_dmonths[10] = 30;
if(leapYear(year)) i_dmonths[1] = 29;
else i_dmonths[1] = 28;
return i_dmonths[month-1];
}
The function works but my doubt is if it remakes the array each time
that I call the function. If it's so, it's correct the use of a global
variable to understand if the function was initialized before or never.
For example :
bool isInit = false; // global var
int nDaysOnMonth(int month, int year){
if(!isInit){
static int i_dmonths[12];
i_dmonths[0] = i_dmonths[2] = i_dmonths[4] = i_dmonths[6] =
i_dmonths[7] = i_dmonths[9] = i_dmonths[11] = 31;
i_dmonths[3] = i_dmonths[5] = i_dmonths[8] = i_dmonths[10] = 30;
} // end if (!isInit)
if(leapYear(year)) i_dmonths[1] = 29;
else i_dmonths[1] = 28;
return i_dmonths[month-1];
}
.... or the compiler could leave some error ?!
Another question is that I have some list with some events for days and
months and I want to stamp on screen a table with all events for one
month, for each months. So I thought to make a function that contains
an array of 31 list elements (one cell for day). This function will
called more times on the program. So I did something like :
void function_showThisTable(int i_month, int i_year, other arguments
(LISTS)){
// DAYTABLE is a struct, the type of element on the lists...
DAYTABLE** tabs = calloc(31, sizeof(DAYTABLE*));
// etc...
}
1. Could I insert the static before DAYTABLE** (the table is ever of 31
elements, have I to realloc each time the space ?!), if yes hhave I to
use a 'global variable' to understand if the function was inizialized
one time ?!
2. The use of calloc assicure that each pointer (DAYTABLE*) will be
NULL at the initialization ?! ... or have I to do a loop to assign each
cell like NULL.
3. The compiler understands that I want to make an array of pointers or
could give some error if I try to go in some row like tabs[3] or
tabs[5] ?!
Thanks for any answer.
Bye
- Atari
p.s. I'm sorry for my bad english, I'm trying to learn it too
save the content of a variable.
So I want to use it to write a function that return the number of days
in one month. Since the days on one month are even the some, it could
be better to compute the array only one time. And so I did :
int nDaysOnMonth(int month, int year){
static int i_dmonths[12];
i_dmonths[0] = i_dmonths[2] = i_dmonths[4] = i_dmonths[6] =
i_dmonths[7] = i_dmonths[9] = i_dmonths[11] = 31;
i_dmonths[3] = i_dmonths[5] = i_dmonths[8] = i_dmonths[10] = 30;
if(leapYear(year)) i_dmonths[1] = 29;
else i_dmonths[1] = 28;
return i_dmonths[month-1];
}
The function works but my doubt is if it remakes the array each time
that I call the function. If it's so, it's correct the use of a global
variable to understand if the function was initialized before or never.
For example :
bool isInit = false; // global var
int nDaysOnMonth(int month, int year){
if(!isInit){
static int i_dmonths[12];
i_dmonths[0] = i_dmonths[2] = i_dmonths[4] = i_dmonths[6] =
i_dmonths[7] = i_dmonths[9] = i_dmonths[11] = 31;
i_dmonths[3] = i_dmonths[5] = i_dmonths[8] = i_dmonths[10] = 30;
} // end if (!isInit)
if(leapYear(year)) i_dmonths[1] = 29;
else i_dmonths[1] = 28;
return i_dmonths[month-1];
}
.... or the compiler could leave some error ?!
Another question is that I have some list with some events for days and
months and I want to stamp on screen a table with all events for one
month, for each months. So I thought to make a function that contains
an array of 31 list elements (one cell for day). This function will
called more times on the program. So I did something like :
void function_showThisTable(int i_month, int i_year, other arguments
(LISTS)){
// DAYTABLE is a struct, the type of element on the lists...
DAYTABLE** tabs = calloc(31, sizeof(DAYTABLE*));
// etc...
}
1. Could I insert the static before DAYTABLE** (the table is ever of 31
elements, have I to realloc each time the space ?!), if yes hhave I to
use a 'global variable' to understand if the function was inizialized
one time ?!
2. The use of calloc assicure that each pointer (DAYTABLE*) will be
NULL at the initialization ?! ... or have I to do a loop to assign each
cell like NULL.
3. The compiler understands that I want to make an array of pointers or
could give some error if I try to go in some row like tabs[3] or
tabs[5] ?!
Thanks for any answer.
Bye
- Atari
p.s. I'm sorry for my bad english, I'm trying to learn it too