Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #108370 > unrolled thread

Why does 'static' cause an error ?

Started byfl <rxjwg98@gmail.com>
First post2017-04-23 11:17 -0700
Last post2017-04-24 10:16 +0200
Articles 13 — 11 participants

Back to article view | Back to comp.lang.c


Contents

  Why does 'static' cause an error ? fl <rxjwg98@gmail.com> - 2017-04-23 11:17 -0700
    Re: Why does 'static' cause an error ? Noob <root@127.0.0.1> - 2017-04-23 22:00 +0200
      Re: Why does 'static' cause an error ? Richard Heathfield <rjh@cpax.org.uk> - 2017-04-23 22:22 +0100
    Re: Why does 'static' cause an error ? bartc <bc@freeuk.com> - 2017-04-23 21:34 +0100
      Re: Why does 'static' cause an error ? Keith Thompson <kst-u@mib.org> - 2017-04-23 16:45 -0700
        Re: Why does 'static' cause an error ? fl <rxjwg98@gmail.com> - 2017-04-23 17:44 -0700
          Re: Why does 'static' cause an error ? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2017-04-23 20:26 -0700
        Re: Why does 'static' cause an error ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-24 13:14 +0100
          Re: Why does 'static' cause an error ? Nick Bowler <nbowler@draconx.ca> - 2017-04-24 18:20 +0000
    Re: Why does 'static' cause an error ? Jerry Stuckle <jstucklex@attglobal.net> - 2017-04-23 17:52 -0400
      Re: Why does 'static' cause an error ? jacobnavia <jacob@jacob.remcomp.fr> - 2017-04-24 00:57 +0200
    Re: Why does 'static' cause an error ? Keith Thompson <kst-u@mib.org> - 2017-04-23 16:43 -0700
      Re: Why does 'static' cause an error ? David Brown <david.brown@hesbynett.no> - 2017-04-24 10:16 +0200

#108370 — Why does 'static' cause an error ?

Fromfl <rxjwg98@gmail.com>
Date2017-04-23 11:17 -0700
SubjectWhy does 'static' cause an error ?
Message-ID<64abf1f2-1c45-4b1b-95cf-a88133813085@googlegroups.com>
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,

[toc] | [next] | [standalone]


#108371

FromNoob <root@127.0.0.1>
Date2017-04-23 22:00 +0200
Message-ID<odj0v4$rmv$1@dont-email.me>
In reply to#108370
On 23/04/2017 20:17, fl wrote:

> 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.)

[toc] | [prev] | [next] | [standalone]


#108374

FromRichard Heathfield <rjh@cpax.org.uk>
Date2017-04-23 22:22 +0100
Message-ID<odj5p2$de5$1@dont-email.me>
In reply to#108371
On 23/04/17 21:00, Noob wrote:
> On 23/04/2017 20:17, fl wrote:
>
>> 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

[toc] | [prev] | [next] | [standalone]


#108372

Frombartc <bc@freeuk.com>
Date2017-04-23 21:34 +0100
Message-ID<Dd8LA.805271$0k4.555569@fx37.am4>
In reply to#108370
On 23/04/2017 19:17, fl wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#108380

FromKeith Thompson <kst-u@mib.org>
Date2017-04-23 16:45 -0700
Message-ID<lnk26apu2m.fsf@kst-u.example.com>
In reply to#108372
bartc <bc@freeuk.com> writes:
> On 23/04/2017 19:17, fl wrote:
[...]
>> 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])
>> 	    }
>> 	};
[...]
> 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-u@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"

[toc] | [prev] | [next] | [standalone]


#108382

Fromfl <rxjwg98@gmail.com>
Date2017-04-23 17:44 -0700
Message-ID<4d24fc15-489b-467c-8f94-659254b0cf11@googlegroups.com>
In reply to#108380
On Sunday, April 23, 2017 at 7:45:11 PM UTC-4, Keith Thompson wrote:
> bartc <bc@freeuk.com> writes:
> > On 23/04/2017 19:17, fl wrote:
> [...]
> >> 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])
> >> 	    }
> >> 	};
> [...]
> > 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-u@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"

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.

[toc] | [prev] | [next] | [standalone]


#108383

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2017-04-23 20:26 -0700
Message-ID<odjr3a$tul$1@dont-email.me>
In reply to#108382
On 4/23/2017 5:44 PM, fl wrote:
>
> 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

[toc] | [prev] | [next] | [standalone]


#108400

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2017-04-24 13:14 +0100
Message-ID<87vapuypde.fsf@bsb.me.uk>
In reply to#108380
Keith Thompson <kst-u@mib.org> writes:

> bartc <bc@freeuk.com> writes:
>> On 23/04/2017 19:17, fl wrote:
> [...]
>>> 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])
>>> 	    }
>>> 	};
> [...]
>> 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.

[toc] | [prev] | [next] | [standalone]


#108418

FromNick Bowler <nbowler@draconx.ca>
Date2017-04-24 18:20 +0000
Message-ID<odlfls$olp$1@dont-email.me>
In reply to#108400
On Mon, 24 Apr 2017 13:14:05 +0100, Ben Bacarisse wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#108376

FromJerry Stuckle <jstucklex@attglobal.net>
Date2017-04-23 17:52 -0400
Message-ID<odj7gt$hvn$1@jstuckle.eternal-september.org>
In reply to#108370
On 4/23/2017 2:17 PM, fl wrote:
> 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
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#108377

Fromjacobnavia <jacob@jacob.remcomp.fr>
Date2017-04-24 00:57 +0200
Message-ID<odjbba$vpn$1@dont-email.me>
In reply to#108376
Le 23/04/2017 à 23:52, Jerry Stuckle a écrit :
> Is uartTivaRB[0] a compile-time constant?

there it is...

Apparently not

[toc] | [prev] | [next] | [standalone]


#108379

FromKeith Thompson <kst-u@mib.org>
Date2017-04-23 16:43 -0700
Message-ID<lno9vmpu4w.fsf@kst-u.example.com>
In reply to#108370
fl <rxjwg98@gmail.com> writes:
> 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-u@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"

[toc] | [prev] | [next] | [standalone]


#108388

FromDavid Brown <david.brown@hesbynett.no>
Date2017-04-24 10:16 +0200
Message-ID<odkc2k$8of$1@dont-email.me>
In reply to#108379
On 24/04/17 01:43, Keith Thompson wrote:
> fl <rxjwg98@gmail.com> writes:
>> 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).


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web