Discussion:
initializer element is not constant
(too old to reply)
j***@gmail.com
2017-05-04 13:20:07 UTC
Permalink
Raw Message
static void got_char(struct work_struct *work)
{
char fileinfo_buff[200], path[120],character[5];
char scancode = scancode_ref ? *scancode_ref : 0;
int arr_index;
static int check = 0, shift_key = 1;

In the above code, i get an error in the line "char scancode = scancode_ref ? *scancode_ref : 0", specifying initializer element is not constant.

can anybody help me out in this issue.


regards
Kenny McCormack
2017-05-04 13:34:42 UTC
Permalink
Raw Message
Post by j***@gmail.com
static void got_char(struct work_struct *work)
{
char fileinfo_buff[200], path[120],character[5];
char scancode = scancode_ref ? *scancode_ref : 0;
int arr_index;
static int check = 0, shift_key = 1;
In the above code, i get an error in the line "char scancode = scancode_ref ?
*scancode_ref : 0", specifying initializer element is not constant.
can anybody help me out in this issue.
What is 'scancode_ref'? Where is it declared?
--
People who say they'll vote for someone else because Obama couldn't fix
*all* of Bush's messes are like people complaining that he couldn't cure
cancer, so they'll go and vote for (more) cancer.
Ben Bacarisse
2017-05-04 14:02:25 UTC
Permalink
Raw Message
Post by j***@gmail.com
static void got_char(struct work_struct *work)
{
char fileinfo_buff[200], path[120],character[5];
char scancode = scancode_ref ? *scancode_ref : 0;
int arr_index;
static int check = 0, shift_key = 1;
In the above code, i get an error in the line "char scancode =
scancode_ref ? *scancode_ref : 0", specifying initializer element is
not constant.
Are you sure? What compiler, and what options are you using?

You'd get that message if scancode is declared 'static' but what you've
posted looks fine to me.
--
Ben.
James Kuyper
2017-05-05 13:40:28 UTC
Permalink
Raw Message
Post by j***@gmail.com
static void got_char(struct work_struct *work)
{
char fileinfo_buff[200], path[120],character[5];
char scancode = scancode_ref ? *scancode_ref : 0;
int arr_index;
static int check = 0, shift_key = 1;
In the above code, i get an error in the line "char scancode = scancode_ref ? *scancode_ref : 0", specifying initializer element is not constant.
can anybody help me out in this issue.
Under the current standard, this is perfectly legal:

"All the expressions in an initializer for an object that has static or
thread storage duration shall be constant expressions or string
literals." (6.7.9p4)
Since scancode has automatic storage duration, that doesn't apply.
However, I seem to recall that C90 applied this requirement to all
storage durations (which, at that time, did not include thread storage
duration).
If your code must compile with a version of the C standard that was
superceded 17 years ago, there's a trivial work-around:

static void got_char(struct work_struct *work)
{
char fileinfo_buff[200], path[120],character[5];
char scancode;
int arr_index;
static int check = 0, shift_key = 1;
scancode = scancode_ref ? *scancode_ref : 0;
Tim Rentsch
2017-05-05 17:16:51 UTC
Permalink
Raw Message
Post by James Kuyper
Post by j***@gmail.com
static void got_char(struct work_struct *work)
{
char fileinfo_buff[200], path[120],character[5];
char scancode = scancode_ref ? *scancode_ref : 0;
int arr_index;
static int check = 0, shift_key = 1;
In the above code, i get an error in the line "char scancode =
scancode_ref ? *scancode_ref : 0", specifying initializer element is
not constant.
can anybody help me out in this issue.
"All the expressions in an initializer for an object that has static or
thread storage duration shall be constant expressions or string
literals." (6.7.9p4)
Since scancode has automatic storage duration, that doesn't apply.
However, I seem to recall that C90 applied this requirement to all
storage durations (which, at that time, did not include thread storage
duration).
The rule in C90 is that initializers for auto variables were
required to be constant expressions for arrays, structs, and
(IIANM) unions. Initializers for scalar auto variables did not
have to be constant expressions.
Andrey Tarasevich
2017-05-08 05:43:14 UTC
Permalink
Raw Message
Post by j***@gmail.com
static void got_char(struct work_struct *work)
{
char fileinfo_buff[200], path[120],character[5];
char scancode = scancode_ref ? *scancode_ref : 0;
int arr_index;
static int check = 0, shift_key = 1;
In the above code, i get an error in the line "char scancode = scancode_ref ? *scancode_ref : 0", specifying initializer element is not constant.
can anybody help me out in this issue.
No.

Unless one of these identifiers is a macro with a bunch of stuff hidden
inside, the code you posted cannot possibly produce this error. End of
story.
--
Best regards,
Andrey Tarasevich
Loading...