473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

initializer needs constant

struct mystruct {
int a;
int b;
};

struct mystruct x = { 10, 10 }, y = x;

What section in the standard says that y needs a constant?
And why can't it resolve x so that y can have a copy of its values?

--
nethlek
Nov 13 '05 #1
2 1920
ne*****@tokyo.com (Mantorok Redgormor) wrote in
news:41*************************@posting.google.co m:
struct mystruct {
int a;
int b;
};

struct mystruct x = { 10, 10 }, y = x;

What section in the standard says that y needs a constant?
And why can't it resolve x so that y can have a copy of its values?


const int foo = 10;
int bar = foo; <-- error: initializer element is not constant

const int foo = 10;

does not mean foo is a constant, it means the compiler will not allow you
to assign a value to foo other than an initializer. You need a real
constant, not a const variable.

--
- Mark ->
--
Nov 13 '05 #2
In article <41*************************@posting.google.com> , Mantorok Redgormor wrote:
struct mystruct {
int a;
int b;
};

struct mystruct x = { 10, 10 }, y = x;

What section in the standard says that y needs a constant?
And why can't it resolve x so that y can have a copy of its values?


In ISO/IEC 9899:1990 the "Constraints" subsection of the
"6.5.7 Initialization" section contains the sentence:

All the expressions in an initializer for an object that has
static storage duration or in an initializer list for an object
that has aggregate or union type shall be constant
expression.
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Todd Nathan | last post by:
Hi. have this code and compiler problem. GCC 2.95.3, BeOS, error "initializer element is not constant" #ifdef FILEIO { static struct { char *sfn; FILE *sfd; } stdfiles = {
6
by: Clint Olsen | last post by:
What about the following is not computable? It seems that the size of foo is easily computable: typedef struct { char *name; char *data; } Foo; int main(void) {
6
by: Marty M | last post by:
The following causes the "invalid initializer" message during gcc compile time... char firstword = word(question,1); the "word" function is... char * word(char *phrase, int what) {...body...
2
by: David Green | last post by:
Ok, i'm a real c n00b but i needed a piece of code for some work i was doing. Initially i was running the stuff under linux and using gcc to compile the c code and it worked fine but now i need to...
3
by: Levi Campbell | last post by:
Hi, I'm trying to debug an app someone else wrote called eMixer. Here's the log contents: cc -O3 -funroll-loops -c -o main.o main.c cc -O3 -funroll-loops -c -o nctgui.o nctgui.c cc -O3...
3
by: vib | last post by:
Hi there, I wish to get some advice on the problem I face in initializing a table. Here is the code. I've an array of strings, ptrNameString . I want to initialize a table, mySoftKeyTable...
2
by: aarklon | last post by:
Hi all, Recently i was reading a c book which contained a section called C pitfalls. it had a paragraph on the following lines:- ...
5
by: fred | last post by:
Hi, Can someone explain me why gcc-4.0 gives me the 'Initializer element is not constant' error with this code ? Everything seems to be constant here... #include <stdio.h> typedef struct {...
7
by: Paul Edwards | last post by:
On ecgs (gcc) 2.91.57 and on gcc 3.2.3 I am getting the error "initializer element is not constant" on the below code. The code compiles fine with other compilers I have. Is this valid C89 code?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.