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?
extern int *b;
int *a = b;
int main(void)
{
return (0);
}
Thanks. Paul. 7 10153
Sorry folks, the code fails on my other compilers too.
It was this code:
#include <stdio.h>
FILE *a = stderr;
int main(void)
{
return (0);
}
that only failed on gcc. And given that it also fails on ints, I
guess gcc is behaving correctly.
BFN. Paul.
"Paul Edwards" <ke******@nosppaam.w3.towrote in message
news:45**********************@un-2park-reader-01.sydney.pipenetworks.com.au...
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?
extern int *b;
int *a = b;
int main(void)
{
return (0);
}
Thanks. Paul.
On Tue, 24 Oct 2006 14:24:13 +1000, "Paul Edwards"
<ke******@nosppaam.w3.towrote in comp.lang.c:
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?
No, it is not. Initializers for objects with static storage duration,
and that includes all objects defined at file scope, must be constant
expressions. You are trying to initialize the pointer 'a' with the
value of another object. The value of any object, even if it is a
const qualified object, is not a constant expression in C.
extern int *b;
int *a = b;
int main(void)
{
return (0);
}
Even this is not (necessarily) valid C:
int x;
int *const b = &x; /* valid, &x is an address constant */
int *a = b; /* not valid */
The initialization of 'a' is not valid even though the value of 'b' is
known at compile time, which it is not in your case. The value of an
object is just not among the defined constant expressions in the C
standard.
It is actually possible for a compiler to accept this as an extension,
that being the reason for the (necessarily) I wrote above, because the
C standard allows an implementation to "accept other forms of constant
expressions". But I don't know of any implementation that does.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
On Tue, 24 Oct 2006 14:29:37 +1000, "Paul Edwards"
<ke******@nosppaam.w3.towrote in comp.lang.c:
Sorry folks, the code fails on my other compilers too.
It was this code:
#include <stdio.h>
FILE *a = stderr;
int main(void)
{
return (0);
}
that only failed on gcc. And given that it also fails on ints, I
guess gcc is behaving correctly.
This one is a different story. It works on some compilers and fails
on others. It depends on the compiler's definition of the macro
"stderr". The C standard requires that stdin, stdout, and stderr are
macros that expand to pointers to file, and depending on how the
macros are defined, they may or may not be address constants at
compile time.
So you should not use this, because as you found out it may work on
some implementations but not others.
The easy fix is to leave the pointer uninitialized, which means it
gets default initialized to NULL, and in main(), before it is used,
assign it.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
"Jack Klein" <ja*******@spamcop.netwrote in message news:gg********************************@4ax.com...
On Tue, 24 Oct 2006 14:29:37 +1000, "Paul Edwards"
<ke******@nosppaam.w3.towrote in comp.lang.c:
Sorry folks, the code fails on my other compilers too.
It was this code:
#include <stdio.h>
FILE *a = stderr;
int main(void)
{
return (0);
}
that only failed on gcc. And given that it also fails on ints, I
guess gcc is behaving correctly.
This one is a different story. It works on some compilers and fails
on others. It depends on the compiler's definition of the macro
"stderr". The C standard requires that stdin, stdout, and stderr are
macros that expand to pointers to file, and depending on how the
macros are defined, they may or may not be address constants at
compile time.
Hi Jack, thanks for your reply. The C90 standard says that
stdin, stdout and stderr are "expressions of type "pointer to
FILE" that point to FILE objects...". It doesn't say they are
macros.
So you should not use this, because as you found out it may work on
some implementations but not others.
I found out how the working version was implemented and
duplicated it, and it works on gcc as well.
#include <stdio.h>
extern FILE myfiles[];
FILE *a = &myfiles[0];
int main(void)
{
return (0);
}
I see what you mean - it is an address here, rather than a
variable.
The easy fix is to leave the pointer uninitialized, which means it
gets default initialized to NULL, and in main(), before it is used,
assign it.
Yes, I have basically done this now. I'm porting bwbasic
to MVS 3.8.
BFN. Paul.
Paul Edwards wrote:
The C90 standard says that stdin, stdout and stderr are
"expressions of type "pointer to FILE"
that point to FILE objects...".
It doesn't say they are macros.
The first three words of the sentence that you are quoting
from 7.9.1 are: "The macros are ..."
--
pete
pete <pf*****@mindspring.comwrites:
Paul Edwards wrote:
>The C90 standard says that stdin, stdout and stderr are "expressions of type "pointer to FILE" that point to FILE objects...". It doesn't say they are macros.
The first three words of the sentence that you are quoting
from 7.9.1 are: "The macros are ..."
And that's a truly impressive run-on sentence (it starts more than a
page earlier).
The wording does cause some confusion. For all the other macros in
that sentence, the standard uses the phrase "which expands to" or
"which expand to"; for stderr, stdin, and stdout, is says "which are
expression ...".
I raised this a few months ago in comp.std.c: http://groups.google.com/group/comp....54094bb4950930
The consensus was that std{err,in,out} *are* required to be macros.
(The restriction isn't necessarily useful, but there's no point in
allowing the trivial extra flexibility for implementers.)
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"pete" <pf*****@mindspring.comwrote in message news:45***********@mindspring.com...
Paul Edwards wrote:
The C90 standard says that stdin, stdout and stderr are
"expressions of type "pointer to FILE"
that point to FILE objects...".
It doesn't say they are macros.
The first three words of the sentence that you are quoting
from 7.9.1 are: "The macros are ..."
Wow!
BFN. Paul. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 = {
|
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)
{
|
by: Mantorok Redgormor |
last post by:
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...
|
by: Scott |
last post by:
Hi All,
I have the following C code in a header file, outside of any functions:
const float X = 50;
const float Y = 100 * X;
But, when compiling, I get an error:
initializer element is...
|
by: bingfeng |
last post by:
I have some codes generated by perl, in which initialize some huge
struct,such as
PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "",
"configuration on a designated vlan",...
|
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...
|
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...
|
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 {...
|
by: Gowtham |
last post by:
Hi,
I had some C code written which initialized a global variable as:
FILE *yyerfp = stdout;
This used to work fine in older versions of gcc. Now, when I tried to
compile this code (with...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
| |