Bug in MSVC ?

N

Nindi

* Nindi:








In C declarations must come before other statements in the block.

You can fix that by using an inner block (curly braces).

Or you can compile as C++ instead of as C.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -

- Show quoted text -

That Worked !!!

I put the Variable declarations at the begining of the function and it
compiled.

Is that part of the standard ?
 
Z

Zeppe

Alf said:
In C declarations must come before other statements in the block.

You can fix that by using an inner block (curly braces).

Or you can compile as C++ instead of as C.

Alf, you're the man! I feel so stupid not to have noticed that, having
seen that removing the line A.x = 100; it was working! :p

Regards,

Zeppe
 
N

Nindi

* Nindi:








In C declarations must come before other statements in the block.

You can fix that by using an inner block (curly braces).

Or you can compile as C++ instead of as C.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -

- Show quoted text -

Thanks very much . I never actualy knew this. But I do now !!!

I consistent behaviour with gcc -pedantic-errors
 
J

James Kanze

That Worked !!!
I put the Variable declarations at the begining of the function and it
compiled.
Is that part of the standard ?

Yes and no. First, as Alf said, it's a question of C, not C++.
If you really want to compile C, and not C++, you should ask in
comp.lang.c, and not here.

Second, the current C standard does NOT require declarations to
come before other code. But this change was introduced in C99,
and many C compilers, apparently, don't implement the new
standard yet. (FWIW: I seem to recall hearing that Microsoft's
policy with regards to C is just to ignore it---VC++ is sold as
a C++ compiler, and any C support is more or less "as is", left
over from earlier versions of the compiler when they did support
C.)

The reason gcc compiles the code, of course, is that they also
take C seriously, and are moving toward full C99 compliance.
 
R

red floyd

Nindi said:
I cannot get the following code to compile under MSVC 2003 or 2005.

..........................................................................................
#include<stdio.h>

struct _MyStruct;

Your program is ill-formed. Any identifier with a leading underscore
followed by an uppercase letter is reserved to the implementation. You
are not allowed to use them.
typedef struct _MyStruct MyStruct;

This is a C-ism. In C++ you'd just declare

struct MyStruct { /* contents here */ };

And use MyStruct directly.
 
R

red floyd

Nindi said:
Ok even simpler :-

main.c ( NOT main.cpp)

...............................................................
struct MyStruct {double x;};
typedef void (*funcType)(struct MyStruct *);
void MyFunc(struct MyStruct *theStruct){}

int main () {
struct MyStruct A;
A.x = 100.0;
funcType f=&MyFunc;
f(&A);
return 0;

}

.....................................................

Under VC 2003 & 2005 refuses to compile. The Errors are

If you're naming it "main.c", and you're saying it's C code, why are you
posting in comp.lang.c++?
 
P

*PaN!*

They are not part of the solution at all. The only file in the
solution explorer is main.c. Also When i created the project I
selected 'Empty' in the project settings.
Have you been able to compile it as 'main.c'

Honestly, you all are missing something *very* obvious.

int main () {
MyStruct A;
A.x = 100.0;
funcType f=&MyFunc;
f(&A);
}

In C variable declarations after a statement are illegal: declarations in a
function must be grouped *before* any statement.

If you comment A.x you'll get the two variable declarations grouped in the
declaration sections, hence it compiles.

--*PaN!*
 
P

*PaN!*

The reason gcc compiles the code, of course, is that they also
take C seriously, and are moving toward full C99 compliance.

Good to know that they chosen to allow this in C99 =)

--*PaN!*
 
R

red floyd

*PaN!* said:
Honestly, you all are missing something *very* obvious.

Maybe we all missed it because this is a C++ group. If he's got a C
problem, he should post to comp.lang.c.
 
D

Duane Hebert

Nindi said:
They are not part of the solution at all. The only file in the
solution explorer is main.c. Also When i created the project I
selected 'Empty' in the project settings.
Have you been able to compile it as 'main.c'


Why not use .cpp for your extension or change your
ide to accept .c for c++?
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top