Discussion:
Why does 'static' cause an error ?
(too old to reply)
fl
2017-04-23 18:17:17 UTC
Permalink
Hi,

After I copy a code snippet to a test .c project, I have such an error when
I carelessly add a 'static' key word at the beginning.

"28 expression must have a constant value"



static const UART_HWAttrs uartHWAttrs[EK_UARTCOUNT] = {
{
.baseAddr = UART0_BASE,
.intNum = INT_UART0,
.intPriority = (~0),
.flowControl = UART_FLOWCONTROL_NONE,
.ringBufPtr = uartTivaRB[0],
.ringBufSize = sizeof(uartTivaRB[0])
}
};
.............


Can you tell me why 'static' can cause such an error?
What rule does it violate?

Thanks,
Noob
2017-04-23 20:00:33 UTC
Permalink
Post by fl
Can you tell me why 'static' can cause such an error?
What rule does it violate?
The problem is on line 42.

(You're supposed to post an example that compiles.)
Richard Heathfield
2017-04-23 21:22:39 UTC
Permalink
Post by Noob
Post by fl
Can you tell me why 'static' can cause such an error?
What rule does it violate?
The problem is on line 42.
(You're supposed to post an example that compiles.)
When the problem being asked about is a compilation error, it's kind of
tricky to post an example that compiles.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
bartc
2017-04-23 20:34:46 UTC
Permalink
Post by fl
Hi,
After I copy a code snippet to a test .c project, I have such an error when
I carelessly add a 'static' key word at the beginning.
"28 expression must have a constant value"
static const UART_HWAttrs uartHWAttrs[EK_UARTCOUNT] = {
{
.baseAddr = UART0_BASE,
.intNum = INT_UART0,
.intPriority = (~0),
.flowControl = UART_FLOWCONTROL_NONE,
.ringBufPtr = uartTivaRB[0],
.ringBufSize = sizeof(uartTivaRB[0])
}
};
.............
Can you tell me why 'static' can cause such an error?
What rule does it violate?
Presumably one of those expressions isn't a compile time one.

Add // in front of each. The error should go away. Then remove the //s
one by on until it comes back. (Watch out for a trailing comma on the
last uncommented line causing a syntax error.)

(Maybe uartTivarRB isn't a flat array, but a pointer?)
--
Bartc
Keith Thompson
2017-04-23 23:45:05 UTC
Permalink
[...]
Post by bartc
Post by fl
static const UART_HWAttrs uartHWAttrs[EK_UARTCOUNT] = {
{
.baseAddr = UART0_BASE,
.intNum = INT_UART0,
.intPriority = (~0),
.flowControl = UART_FLOWCONTROL_NONE,
.ringBufPtr = uartTivaRB[0],
.ringBufSize = sizeof(uartTivaRB[0])
}
};
[...]
Post by bartc
Presumably one of those expressions isn't a compile time one.
Add // in front of each. The error should go away. Then remove the //s
one by on until it comes back. (Watch out for a trailing comma on the
last uncommented line causing a syntax error.)
(Maybe uartTivarRB isn't a flat array, but a pointer?)
It doesn't matter. uartTivaRB[0] isn't a constant expression even if
uartTivarRB is a "flat array". See N1570 6.6.
--
Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
fl
2017-04-24 00:44:28 UTC
Permalink
Post by Keith Thompson
[...]
Post by bartc
Post by fl
static const UART_HWAttrs uartHWAttrs[EK_UARTCOUNT] = {
{
.baseAddr = UART0_BASE,
.intNum = INT_UART0,
.intPriority = (~0),
.flowControl = UART_FLOWCONTROL_NONE,
.ringBufPtr = uartTivaRB[0],
.ringBufSize = sizeof(uartTivaRB[0])
}
};
[...]
Post by bartc
Presumably one of those expressions isn't a compile time one.
Add // in front of each. The error should go away. Then remove the //s
one by on until it comes back. (Watch out for a trailing comma on the
last uncommented line causing a syntax error.)
(Maybe uartTivarRB isn't a flat array, but a pointer?)
It doesn't matter. uartTivaRB[0] isn't a constant expression even if
uartTivarRB is a "flat array". See N1570 6.6.
--
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Thanks to all of you. Excuse me I didn't post the more complete code
snippet at first. The error is caused the static code line inside main().
If I put the definition out of main(), it has no such an error, which
can be calculated.
Andrey Tarasevich
2017-04-24 03:26:28 UTC
Permalink
Post by fl
The error is caused the static code line inside main().
If I put the definition out of main(), it has no such an error, which
can be calculated.
I find it hard to believe. If this code does not compile inside 'main',
it is not supposed to compile outside of 'main' either.
--
Best regards,
Andrey Tarasevich
Ben Bacarisse
2017-04-24 12:14:05 UTC
Permalink
Post by Keith Thompson
[...]
Post by bartc
Post by fl
static const UART_HWAttrs uartHWAttrs[EK_UARTCOUNT] = {
{
.baseAddr = UART0_BASE,
.intNum = INT_UART0,
.intPriority = (~0),
.flowControl = UART_FLOWCONTROL_NONE,
.ringBufPtr = uartTivaRB[0],
.ringBufSize = sizeof(uartTivaRB[0])
}
};
[...]
Post by bartc
Presumably one of those expressions isn't a compile time one.
Add // in front of each. The error should go away. Then remove the //s
one by on until it comes back. (Watch out for a trailing comma on the
last uncommented line causing a syntax error.)
(Maybe uartTivarRB isn't a flat array, but a pointer?)
It doesn't matter. uartTivaRB[0] isn't a constant expression even if
uartTivarRB is a "flat array". See N1570 6.6.
But uartTivaRB might be a 2D array. I know it's unlikely, but with no
further information uartTivaRB[0] is not ruled out as an initializer by
6.6.

The final part of the last sentence about address constants is not 100%
clear (to me) but I don't think it applies here. It says

"The array-subscript [] ... may be used in the creation of an address
constant, but the value of an object shall not be accessed by use of
these operators."

If uartTivaRB[0] has array type, I don't think the [] operator accesses
the value of an object.
--
Ben.
Nick Bowler
2017-04-24 18:20:44 UTC
Permalink
Post by Ben Bacarisse
But uartTivaRB might be a 2D array. I know it's unlikely, but with no
further information uartTivaRB[0] is not ruled out as an initializer by
6.6.
The final part of the last sentence about address constants is not 100%
clear (to me) but I don't think it applies here. It says
"The array-subscript [] ... may be used in the creation of an address
constant, but the value of an object shall not be accessed by use of
these operators."
If uartTivaRB[0] has array type, I don't think the [] operator accesses
the value of an object.
6.3.2.1p2 quite clearly states how non-array lvalues are "converted to
the value stored in the designated object", which clearly counts as
reading that value and therefore is definitely an "access" per 3.1p1.

But this doesn't apply to arrays, 6.3.2.1p3 does, which explains how
array lvalues get converted to a pointer to the array's first element.
In this case, there is no statement whatsoever regarding object values.

So I would agree that, due to lack of any statement to the contrary,
the value of an array object is not read (and certainly not modified)
when an lvalue designating that array decays into a pointer.
Therefore, such decay is not an "access" per 3.1p1.

Jerry Stuckle
2017-04-23 21:52:28 UTC
Permalink
Post by fl
Hi,
After I copy a code snippet to a test .c project, I have such an error when
I carelessly add a 'static' key word at the beginning.
"28 expression must have a constant value"
static const UART_HWAttrs uartHWAttrs[EK_UARTCOUNT] = {
{
.baseAddr = UART0_BASE,
.intNum = INT_UART0,
.intPriority = (~0),
.flowControl = UART_FLOWCONTROL_NONE,
.ringBufPtr = uartTivaRB[0],
.ringBufSize = sizeof(uartTivaRB[0])
}
};
.............
Can you tell me why 'static' can cause such an error?
What rule does it violate?
Thanks,
Is uartTivaRB[0] a compile-time constant?
--
==================
Remove the "x" from my email address
Jerry Stuckle
***@attglobal.net
==================
jacobnavia
2017-04-23 22:57:44 UTC
Permalink
Post by Jerry Stuckle
Is uartTivaRB[0] a compile-time constant?
there it is...

Apparently not
Keith Thompson
2017-04-23 23:43:43 UTC
Permalink
Post by fl
After I copy a code snippet to a test .c project, I have such an error when
I carelessly add a 'static' key word at the beginning.
"28 expression must have a constant value"
static const UART_HWAttrs uartHWAttrs[EK_UARTCOUNT] = {
{
.baseAddr = UART0_BASE,
.intNum = INT_UART0,
.intPriority = (~0),
.flowControl = UART_FLOWCONTROL_NONE,
.ringBufPtr = uartTivaRB[0],
.ringBufSize = sizeof(uartTivaRB[0])
}
};
.............
Can you tell me why 'static' can cause such an error?
What rule does it violate?
The error message told you that the initializers have to be constant
expressions. An indexing operation is not a constant expression (even
if it refers to an array object with constant initialization).

A simpler example:

const int arr[] = { 10, 20 };
const int x = arr[0];

The initializer for x is invalid.
--
Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
David Brown
2017-04-24 08:16:18 UTC
Permalink
Post by Keith Thompson
Post by fl
After I copy a code snippet to a test .c project, I have such an error when
I carelessly add a 'static' key word at the beginning.
"28 expression must have a constant value"
static const UART_HWAttrs uartHWAttrs[EK_UARTCOUNT] = {
{
.baseAddr = UART0_BASE,
.intNum = INT_UART0,
.intPriority = (~0),
.flowControl = UART_FLOWCONTROL_NONE,
.ringBufPtr = uartTivaRB[0],
.ringBufSize = sizeof(uartTivaRB[0])
}
};
.............
Can you tell me why 'static' can cause such an error?
What rule does it violate?
The error message told you that the initializers have to be constant
expressions. An indexing operation is not a constant expression (even
if it refers to an array object with constant initialization).
I would also guess that the initialisation is incorrect, and should be:

.ringBufPtr = &uartTivaRB[0],

(or just ".ringBufPtr = uartTivaRB,", but presumably someone thought it
was helpful to write it with the index).
Loading...