EmbeddedRelated.com
Forums

Error: Constant initializer expected

Started by Frank March 26, 2012
Hi
I have an problem, I get an error - Constant initializer expected.

Can some one help me.

Thanks
Frank
#define LONGITUDE -74.74325180053711
static struct UB {
float Longitude;
}UB;

#web UB.Longitude
//**I get error here UB.Latitude**//
const float latitude2 = UB.Latitude;

On 3/26/2012 8:39 PM, Frank wrote:
> //**I get error here UB.Latitude**//
> const float latitude2 = UB.Latitude;

UB.Lattitiude is not a constant and is being assigned to a constant.

I still don't thing DC allows variables to be assigned a value at
definition unless it is const and assigned a const that are bot known at
compile time.
--
------
Scott G. Henion, Consultant
Web site: http://SHDesigns.org
------
Thanks Scott

Is there a way I could do this.
On 3/26/2012 8:51 PM, Frank wrote:
> Thanks Scott
>
> Is there a way I could do this.
>

You did not say where lattitude2 is defined.

You could do the assignment at the start of main() or use a GLOBAL_INIT{}

Other issue is when is UB.Latitiude assigned a value?

Hard to say much without knowing the scope and where the variables are used.

--
------
Scott G. Henion, Consultant
Web site: http://SHDesigns.org
------

Unless you're using DC 10.xx, you cannot initialize
a variable on the same line as the declaration.

Also, there is no "latitude" element in the structure.
Thanks Scott
What I am trying to do is save variables to flash, then I can change a value on a web page and save back in flash, this value is used in an algorithm to track the sun.
thanks
On 3/26/2012 9:25 PM, Frank wrote:
> Thanks Scott
> What I am trying to do is save variables to flash, then I can change a value on a web page and save back in flash, this value is used in an algorithm to track the sun.

Well they would not be const if they can be changed.

Easiest way to save/restore variables in flash is to use the user block.

Even easier is to to use battery-backed RAM if your board has battery
backup to the RAM.

--
------
Scott G. Henion, Consultant
Web site: http://SHDesigns.org
------

I have a hard time to figure out how to to that.
I have an BL2600 board
On Mar 26, 2012, at 5:39 PM, Frank wrote:
> //**I get error here UB.Latitude**//
> const float latitude2 = UB.Latitude;

Frank,

You can't assign the value of a variable to a constant. What are you trying to accomplish? It's usually easier to use a macro for a constant integer or float, than to have an actual constant variable.

Dynamic C 9 (and earlier) didn't follow the ANSI standard for "const", and I can't quite recall its exact quirks.

In Dynamic C 10, a statement like "const float latitude2 = 1.2345;" results in the compiler creating a variable called "latitude2", set to 1.2345, and disallowing any code that would change it. It also requires that the initializer (the 1.2345) not reference the values of any other constants -- just literal values.

-Tom