Hello all
consider the code fragment below:
case 1:
{
int i = 1;
break;
}
VC give this error:error C2360: initialization of 'i' is skipped by
'case' label
but if i write like this:
case 1:
{
int i;
i = 1;
break;
}
it is successfully compiled
why ? 5 7448
"Teddy" <du********@hot mail.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. . Hello all
consider the code fragment below: case 1: { int i = 1; break; }
VC give this error:error C2360: initialization of 'i' is skipped by 'case' label
but if i write like this: case 1: { int i; i = 1; break; }
it is successfully compiled
why ?
Well, what happens when somewhere down the line, after the switch, or even
in a differant case, the value of i is queried? e.g. x = i; Unless case 1 is
guarenteed then there is no such thing as i. Because case 1 is the only
block aware of i's existance at all. In the second code snipper there is
always an i. The compiler is smart enough to know this.
On 2005-05-30, Teddy <du********@hot mail.com> wrote: Hello all
consider the code fragment below: case 1: { int i = 1; break; }
Are you sure you did exactly the above, and not something like this:
switch (i) {
case 1:
int i = 1;
break;
case 2: // skips initialization of i, but i still has scope!
// is the variable i initialized or not ? it's in limbo state
This should give such an error, but the code you posted shouldn't.
VC give this error:error C2360: initialization of 'i' is skipped by 'case' label
but if i write like this: case 1: { int i; i = 1; break; }
it is successfully compiled
Because i is not initialized.
Cheers,
--
Donovan Rebbechi http://pegasus.rutgers.edu/~elflord/
On 2005-05-30, Christopher <an**@nospam.ne t> wrote: Well, what happens when somewhere down the line, after the switch, or even in a differant case, the value of i is queried? e.g. x = i;
In the code he posted, i has block scope. So there's no ambiguity -- if that
happens, it's an "undeclared identifier".
Unless case 1 is guarenteed then there is no such thing as i.
What about this :
if (flag == 0) {
int i = 0;
} else if ( flag == 1) {
int j = 0;
} else if ( flag == 2) {
int k = 0;
}
if flag is set to 0, then there is no such thing as i either.
Because case 1 is the only block aware of i's existance at all.
Likewise above: only the first block knows about i. Yet it is legal.
In the second code snipper there is always an i. The compiler is smart enough to know this.
Are you saying that block scope is only allowed if the compiler knows
whether or not that block is executed ?
Cheers,
--
Donovan Rebbechi http://pegasus.rutgers.edu/~elflord/
"Christophe r" <an**@nospam.ne t> wrote in message
news:mI******** **********@torn ado.texas.rr.co m... "Teddy" <du********@hot mail.com> wrote in message news:11******** **************@ g44g2000cwa.goo glegroups.com.. . Hello all
consider the code fragment below: case 1: { int i = 1; break; }
VC give this error:error C2360: initialization of 'i' is skipped by 'case' label
but if i write like this: case 1: { int i; i = 1; break; }
it is successfully compiled
why ?
Using what you gave as example, I compiled and excuted successfully with
VC++ 6.0 and MinGW 3.4.2
You will need to give fully failing program in order for checking it here.
#include <iostream>
#include <ostream>
int main( )
{
int j = 1;
switch (j)
{
case 1:
{
int i = 1;
break;
}
}
std::cout << j << std::endl;
return 0;
}
--
Gary
It was my fault
I posted the wrong code This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Vinay Aggarwal |
last post by:
I have been thinking about the lazy initialization and double checked
locking problem. This problem is explain in detail here
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
I am not fully convinced that this problem cannot be
solved. I am going to propose a solution here. For the sake of
discussion I will post my solution here. It is possible that the
proposed solution does not work, feedback and comments are...
|
by: Venkat |
last post by:
Hello All.
I am having a trouble creating a Object in Case Label, and calling the
Member Function outside the Switch. Can some one please help me..??
My code looks like
int main()
{
int number;
|
by: Marcelo Pinto |
last post by:
Hi,
Consider the following code snipet:
void f(const std::string & msg)
{
//...
for (...)
{
|
by: gyan |
last post by:
follwing code gives error:
1 #include<iostream.h>
2 int main()
3 {
4 int a=5,b;
5 switch(a){
6 case 1:
7 {b=5;
8 break;
9 }
|
by: giulianodammando |
last post by:
In the development of a simple numerical simulation software i need to
read
some initialization parameters from a file that looks like:
# Global Setup
species = 1;
\begin{specie}<1>
name = NITROGEN;
| |
by: lovecreatesbea... |
last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the
initialization is done once only, conceptually before the program
starts executing, ... .
"Non-automatic variables are initialized before the program starts
executing." -- What does this mean? What is the name of the stage in
which the mentioned initialization is performed? Compile-time or
run-time?
In the following snippet, variables b and c are defined at line 7...
|
by: Jesper Schmidt |
last post by:
When does CLR performs initialization of static variables in a class
library?
(1) when the class library is loaded
(2) when a static variable is first referenced
(3) when...
It seems that (1) holds for unmanaged C++ code, but not for managed
code. I have class library with both managed and unmanaged static
variables that are not referenced by any part of the program. All the
|
by: clairelee0322 |
last post by:
My program has an error C2360: initialization of 'i' is skipped by 'case' label.
I don't know why. Please help me out!
thks!!
the error occurs in Division case 3.
//Claire's Calculator
#include <iostream.h>
#include <math.h>
|
by: Rahul |
last post by:
Hi Everyone,
I have the following code,
class A
{
public : A()
{
}
void show()
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |