I have used a function call to iniatialize a static const variable and a co-worker says it is not legit.
My sparring partner says it would not even be legit if I remove the const because he claims the rh value must be known at compile time and he cites this link: Static Variable Must Be Constant?.
I am not an ANSI standard expert but somewhere in my readings I am sure I have read that this is allowable. Here is a simplified example of what I have done.
Code:
#include"stdafx.h"
int f1(void)
{
printf("\nEnter a character: ");
return getchar();
}
void main(void)
{
int i = 1;
static const char c = f1();
printf("\nf1() returned: %c", c);
/* Next line would gen compile error.
c = 'a';
*/
getchar(); // Pauses output window.
}
The first question that someone will ask is do I really need a function call if it is a constant. Technically no, but the function returns a constant pointer from power loss memory and the code to do so is the better part of a pageful and it is already nicely packaged in a function so the pragmatic answer is yes.
I would appreciate your comments and maybe the answer will surprise more folks than just me.
Thanks,
jvh