Compilation Error?

V

vikascoder

Consider the following code snippet..

#include<iostream>
using namespace std;

int func(void);

int main() {
cout<<"Result :"<<func()<<endl;
}

int func (void) {
int result = 0;
if (true) {
result = 1;
goto cleaup:

}

int i = 0; // i initialize a dummy here.. Error in XCode or Gcc !

cleanup:
return (result);
}

If i compile the above code with Visual Studio 6.0 compiler or
CodeWarrior (Mac) it compiles without any error, but if i compile this
code using gcc or XCode (Mac) it gives the following error..
error: jump to label 'cleanup'
error: from here
error: crosses initialization of 'int i'

The only way to solve this problem is to move the 'int i' definition
below the 'int result' definition... is there any other way??
 
J

Jonathan Mcdougall

Consider the following code snippet..

#include<iostream>
using namespace std;

int func(void);

int main() {
cout<<"Result :"<<func()<<endl;
}

int func (void) {
int result = 0;
if (true) {
result = 1;
goto cleaup:

}

int i = 0; // i initialize a dummy here.. Error in XCode or Gcc !

cleanup:
return (result);
}

If i compile the above code with Visual Studio 6.0 compiler or
CodeWarrior (Mac) it compiles without any error, but if i compile this
code using gcc or XCode (Mac) it gives the following error..
error: jump to label 'cleanup'
error: from here
error: crosses initialization of 'int i'

"It is possible to transfer into a block, but not in a way that
bypasses declarations with initialization. A program that jumps from a
point where a local variable with automatic storage duration is not in
scope to a point where it is in scope is ill-formed unless the variable
has POD type (3.9) and is declared without an initializer (8.5)."
(6.7§3)
The only way to solve this problem is to move the 'int i' definition
below the 'int result' definition... is there any other way??

Don't use goto?


Jonathan
 
I

Ian Collins

Consider the following code snippet..

#include<iostream>
using namespace std;

int func(void);

int main() {
cout<<"Result :"<<func()<<endl;
}

int func (void) {
int result = 0;
if (true) {
result = 1;
goto cleaup:

}

int i = 0; // i initialize a dummy here.. Error in XCode or Gcc !

cleanup:
return (result);
}

If i compile the above code with Visual Studio 6.0 compiler or
CodeWarrior (Mac) it compiles without any error, but if i compile this
code using gcc or XCode (Mac) it gives the following error..
error: jump to label 'cleanup'
error: from here
error: crosses initialization of 'int i'
I'm surprised this compiles with any compiler.
The only way to solve this problem is to move the 'int i' definition
below the 'int result' definition... is there any other way??
Sack whoever wrote this rubbish.
 
R

Raj

it shudnt have worked with any of the compiler as far as myknowledge is
concerned.. try the latest compiler version n u'll catch the error..
the only workout is : goto is a bad practice.. stop using it..
 
I

Ian Collins

Raj said:
it shudnt have worked with any of the compiler as far as myknowledge is
concerned.. try the latest compiler version n u'll catch the error..
the only workout is : goto is a bad practice.. stop using it..
Are you answering me or the OP?

Please quote context (see <http://cfaj.freeshell.org/google/>) in your
replies and avoid txt speak.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top