473,378 Members | 1,527 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,378 software developers and data experts.

integer constant expression

why is
Expand|Select|Wrap|Line Numbers
  1.  ("abcd"[1]) 
not an integer constant expression?
Jun 9 '12 #1

✓ answered by donbock

String literals aren't ready to be used as arrays until translation phase 7. It is not unreasonable to conclude that the "syntactic and semantic analysis" in translation phase 7 is when case argument error messages are generated. Perhaps the issue is that the Standard doesn't mandate the order of translation phase 7 activities. Perhaps the error will go away if you use a different compiler. If so, then that would be relying on implementation-defined behavior, which is not a good idea.

From the C99 Standard (regarding string literals and constant expressions):
6.4.5 String literals
...
4 In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and wide string literal tokens are concatenated into a single multibyte character sequence. If any of the tokens are wide string literal tokens, the resulting multibyte character sequence is treated as a wide string literal, otherwise it is treated as a character string literal.
5 In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal of literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence; for wide string literals, the array elements have type wchar_t, and are initialized with the sequence of wide characters corresponding to the multibyte character sequence, as defined by the mbstowcs function with an implementation-defined current locale. The value of a string literal containing a multibyte character or escape sequence not represented in the execution character set is implementation-defined.

6.6 Constant expressions
...
2 A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be.
...
6 An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.
From the C99 Standard (regarding translation phases):
5.1.1.2 Translation phases
The precedence among the syntax rules of translation is specified by the following phases.
...
5. Each source character set member and escape sequence in character constants and string literals is converted to the corresponding member of the execution character set; if there is no corresponding member, it is converted to an implementation-defined member other than the null (wide) character.
6. Adjacent string literal tokens are concatenated.
7. White-space characters separating tokens are no longer significant. Each preprocessing token is converted into a token. The resulting tokens are syntactically and semantically analyzed and translated as a translation unit.
8. All external object and function references are resolved. Library components are linked to satisfy external references to functions and objects not defined in the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment.
By the way, what are you trying to accomplish with "switch (0)"?

6 5667
weaknessforcats
9,208 Expert Mod 8TB
I have no idea. Should compile just fine. Your error may be elsewhere in your code.
Jun 9 '12 #2
@weaknessforcats
when I compile
Expand|Select|Wrap|Line Numbers
  1.   switch (0) {
  2.       case 1: break;
  3.       case  ("abcd"[1]): break;
  4.    }
  5.  
I get "error: an array reference cannot appear in a constant-expression"
Jun 13 '12 #3
You can only put constant values in case statements. Your compiler considers that "abcd"[1] is not a constant. But you can still do a case 'b' instead.
Jun 13 '12 #4
donbock
2,426 Expert 2GB
String literals aren't ready to be used as arrays until translation phase 7. It is not unreasonable to conclude that the "syntactic and semantic analysis" in translation phase 7 is when case argument error messages are generated. Perhaps the issue is that the Standard doesn't mandate the order of translation phase 7 activities. Perhaps the error will go away if you use a different compiler. If so, then that would be relying on implementation-defined behavior, which is not a good idea.

From the C99 Standard (regarding string literals and constant expressions):
6.4.5 String literals
...
4 In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and wide string literal tokens are concatenated into a single multibyte character sequence. If any of the tokens are wide string literal tokens, the resulting multibyte character sequence is treated as a wide string literal, otherwise it is treated as a character string literal.
5 In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal of literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence; for wide string literals, the array elements have type wchar_t, and are initialized with the sequence of wide characters corresponding to the multibyte character sequence, as defined by the mbstowcs function with an implementation-defined current locale. The value of a string literal containing a multibyte character or escape sequence not represented in the execution character set is implementation-defined.

6.6 Constant expressions
...
2 A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be.
...
6 An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.
From the C99 Standard (regarding translation phases):
5.1.1.2 Translation phases
The precedence among the syntax rules of translation is specified by the following phases.
...
5. Each source character set member and escape sequence in character constants and string literals is converted to the corresponding member of the execution character set; if there is no corresponding member, it is converted to an implementation-defined member other than the null (wide) character.
6. Adjacent string literal tokens are concatenated.
7. White-space characters separating tokens are no longer significant. Each preprocessing token is converted into a token. The resulting tokens are syntactically and semantically analyzed and translated as a translation unit.
8. All external object and function references are resolved. Library components are linked to satisfy external references to functions and objects not defined in the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment.
By the way, what are you trying to accomplish with "switch (0)"?
Jun 14 '12 #5
Nothing... that was just a simple way of showing, in response to the first reply, that "abcd"[1] is *not* recognised (by gcc) as a constant integer expression. All this stuff is really in a too-complex-to-post-macro.

Thanks for the detailed response.
Jun 29 '12 #6
@donbock :
where did you get that C++ C99 standard ? I tried to get it (rev 2003) from the ISO certification web site (free since the new standard came out) but the document download fails...

thanks for your answer :)
Jun 29 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

29
by: SysSpider | last post by:
Hi again, This is my problem: when i try to compile the code that contains the function below, i get this: -- gcc:21: error: case label does not reduce to an integer constant gcc:24: error:...
6
by: | last post by:
The following code snippet can be build in VC 6.0, but failed in VC 2003. //////////////save the following code in t.cpp #define _MT #define _WIN32_WINNT 0x0500 #include <iostream> #include...
13
by: devdatta_clc | last post by:
Hi C experts I've a bunch of questions. Consider this simplified piece of code. const int a = 10; int main () { static int b = a;
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
3
by: Old Wolf | last post by:
The code is: extern int x; char *ptr1 = 8 + (char *)&x; char *ptr2 = (char *)(8 + (unsigned)&x); My understanding is that the ptr1 declaration is correct but the ptr2 is not, and the...
1
by: ashiremath | last post by:
Hi All Below the code giving me the error is Constant expression is required. Please help me for this program. Private Sub Send_InputChoice() Dim Input_String As Integer ...
3
by: Dan Smithers | last post by:
What constitutes a constant-expression? I know that it is something that can be determined at compile time. I am trying to use template code and keep getting compiler errors "error: cannot...
5
by: Rex Mottram | last post by:
Can anyone explain this? % cat t.c #include <stdio.h> #define FIRST "first" #define SECOND "second" int main(int argc, char *argv)
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: 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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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 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.