Bug in MSVC ?

N

Nindi

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

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

struct _MyStruct;
typedef struct _MyStruct MyStruct;

typedef void (*funcType)(MyStruct *);

struct _MyStruct {double x;};


void MyFunc(MyStruct *theStruct){printf(" %f ",theStruct->x);}

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

}
................................................................................

Under gcc , no problem .
 
S

SirMike

Dnia 18 May 2007 02:50:05 -0700, Nindi napisa³(a):
I cannot get the following code to compile under MSVC 2003 or 2005.

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

struct _MyStruct;
typedef struct _MyStruct MyStruct;

typedef void (*funcType)(MyStruct *);

struct _MyStruct {double x;};


void MyFunc(MyStruct *theStruct){printf(" %f ",theStruct->x);}

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

}
...............................................................................

Under gcc , no problem .

Give us some errors from the compiler. I see no "return 0" here but don't
know whether you forgot to write it here or is just the snippet.
 
K

Keith Halligan

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

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

struct _MyStruct;
typedef struct _MyStruct MyStruct;

typedef void (*funcType)(MyStruct *);

struct _MyStruct {double x;};

void MyFunc(MyStruct *theStruct){printf(" %f ",theStruct->x);}

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

}

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

Under gcc , no problem .

What compilation errors are you getting?

I just compiled it on MS VC++ 2003 and it compiles fine.
 
N

Nindi

Dnia 18 May 2007 02:50:05 -0700, Nindi napisa³(a):










Give us some errors from the compiler. I see no "return 0" here but don't
know whether you forgot to write it here or is just the snippet.

--
SirMike -http://www.sirmike.org

C makes it easy to shoot yourself in the foot; C++ makes it harder, but
when you do, it blows away your whole leg. - Bjarne Stroustrup- Hide quoted text -

- Show quoted text -



I am not sure whether return is neccessary in main, but I aggree its
better form to put it in.
Here are the errors under 2003
..............................................................
------ Build started: Project: TestStruct, Configuration: Debug Win32
------

Compiling...
main.c
c:\TEMP\TestBed\TestStruct\main.c(18) : error C2275: 'funcType' :
illegal use of this type as an expression
c:\TEMP\TestBed\TestStruct\main.c(8) : see declaration of
'funcType'
c:\TEMP\TestBed\TestStruct\main.c(18) : error C2146: syntax error :
missing ';' before identifier 'f'
c:\TEMP\TestBed\TestStruct\main.c(18) : error C2144: syntax error :
'<Unknown>' should be preceded by '<Unknown>'
c:\TEMP\TestBed\TestStruct\main.c(18) : error C2144: syntax error :
'<Unknown>' should be preceded by '<Unknown>'
c:\TEMP\TestBed\TestStruct\main.c(18) : error C2143: syntax error :
missing ';' before 'identifier'
c:\TEMP\TestBed\TestStruct\main.c(18) : error C2065: 'f' : undeclared
identifier
c:\TEMP\TestBed\TestStruct\main.c(18) : warning C4047: '=' : 'int'
differs in levels of indirection from 'void (__cdecl *)(MyStruct *)'
c:\TEMP\TestBed\TestStruct\main.c(19) : error C2063: 'f' : not a
function

Build log was saved at "file://c:\Temp\TestBed\TestStruct\Debug
\BuildLog.htm"
TestStruct - 7 error(s), 1 warning(s)

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


And under gcc 3.2.3 (mingw) not a peep, it compiles and works fine.
 
N

Nindi

I am not sure whether return is neccessary in main, but I aggree its
better form to put it in.
Here are the errors under 2003
.............................................................
------ Build started: Project: TestStruct, Configuration: Debug Win32
------

Compiling...
main.c
c:\TEMP\TestBed\TestStruct\main.c(18) : error C2275: 'funcType' :
illegal use of this type as an expression
c:\TEMP\TestBed\TestStruct\main.c(8) : see declaration of
'funcType'
c:\TEMP\TestBed\TestStruct\main.c(18) : error C2146: syntax error :
missing ';' before identifier 'f'
c:\TEMP\TestBed\TestStruct\main.c(18) : error C2144: syntax error :
'<Unknown>' should be preceded by '<Unknown>'
c:\TEMP\TestBed\TestStruct\main.c(18) : error C2144: syntax error :
'<Unknown>' should be preceded by '<Unknown>'
c:\TEMP\TestBed\TestStruct\main.c(18) : error C2143: syntax error :
missing ';' before 'identifier'
c:\TEMP\TestBed\TestStruct\main.c(18) : error C2065: 'f' : undeclared
identifier
c:\TEMP\TestBed\TestStruct\main.c(18) : warning C4047: '=' : 'int'
differs in levels of indirection from 'void (__cdecl *)(MyStruct *)'
c:\TEMP\TestBed\TestStruct\main.c(19) : error C2063: 'f' : not a
function

Build log was saved at "file://c:\Temp\TestBed\TestStruct\Debug
\BuildLog.htm"
TestStruct - 7 error(s), 1 warning(s)

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

And under gcc 3.2.3 (mingw) not a peep, it compiles and works fine.- Hide quoted text -

- Show quoted text -


But the following compiles fine
..............................................................
#include<stdio.h>

struct _MyStruct;
typedef struct _MyStruct MyStruct;

typedef void (*funcType)(MyStruct *);

struct _MyStruct {double x;};


void MyFunc(MyStruct *theStruct){printf(" %f ",theStruct->x);}

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

}
.....................................................
 
N

Nindi

It works fine in MSVC2005 to me.

Regards,

Zeppe

This is REALLY wierd !!!
If the source file is called main.cpp it works fine BUT if it is
called main.c it fails
 
K

Keith Halligan

This is REALLY wierd !!!
If the source file is called main.cpp it works fine BUT if it is
called main.c it fails

As far as I can remember if the extension is just .c, msvc will invoke
the C compiler as opposed to the C++ compiler.

If you go into options I think you can set the compiler to invoke for
your project, I think its under advanced or something like that.

The reason you're getting so many failures is that your project/
solution probably has stdafx.h and that is including one of the
standard C++ header files that cannot be compiled by the C compiler.
Remove stdafx.cpp and stdafx.h from the project and recompile.
 
N

Nindi

As far as I can remember if the extension is just .c, msvc will invoke
the C compiler as opposed to the C++ compiler.

If you go into options I think you can set the compiler to invoke for
your project, I think its under advanced or something like that.

The reason you're getting so many failures is that your project/
solution probably has stdafx.h and that is including one of the
standard C++ header files that cannot be compiled by the C compiler.
Remove stdafx.cpp and stdafx.h from the project and recompile.

I am not including any othet header file, the project was set up as an
empty console project
 
K

Keith Halligan

I am not including any othet header file, the project was set up as an
empty console project

Look in the solution explorer and see if the files exist, if they do,
then remove them from the solution. You're not including the files
but thats not to say that the compiler isn't compiling these files.
 
Z

Zeppe

Keith said:
Look in the solution explorer and see if the files exist, if they do,
then remove them from the solution. You're not including the files
but thats not to say that the compiler isn't compiling these files.

If you look at the errors you will see that they are not related to any
stdafx. The weird thing is that the error disappear commenting the line
A.x = 100.0;. I'm usually *very* reluctant in considering bugs in the
compiler, but in this case I really can't understand this behaviour.

Regards,

Zeppe
 
N

Nindi

Look in the solution explorer and see if the files exist, if they do,
then remove them from the solution. You're not including the files
but thats not to say that the compiler isn't compiling these files.- Hide quoted text -

- Show quoted text -

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'
 
R

Ron Natalie

Nindi said:
struct _MyStruct;

Identifiers beginninig with _ and followed by
an uppercase letter are universally reserved to
the implementation. Do not do this.
 
N

Nindi

Identifiers beginninig with _ and followed by
an uppercase letter are universally reserved to
the implementation. Do not do this.

ok ... I haven't done much C and tend to stick to C++. I picked up
this practice of nameing structs _xxxxx and then typedef _xxxxx xxxxxx
from Glib
 
N

Nindi

ok ... I haven't done much C and tend to stick to C++. I picked up
this practice of nameing structs _xxxxx and then typedef _xxxxx xxxxxx
from Glib

main.c
..............................................................
struct struct_MyStruct;
typedef struct struct_MyStruct MyStruct;

typedef void (*funcType)(MyStruct *);

struct struct_MyStruct {double x;};


void MyFunc(MyStruct *theStruct){}

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

}
.................................................................
 
N

Nindi

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

..................................................................
Compiling...
main.c
c:\TEMP\TestBed\TestStruct\main.c(12) : error C2275: 'funcType' :
illegal use of this type as an expression
c:\TEMP\TestBed\TestStruct\main.c(4) : see declaration of 'funcType'
c:\TEMP\TestBed\TestStruct\main.c(12) : error C2146: syntax error :
missing ';' before identifier 'f'
c:\TEMP\TestBed\TestStruct\main.c(12) : error C2144: syntax error :
'<Unknown>' should be preceded by '<Unknown>'
c:\TEMP\TestBed\TestStruct\main.c(12) : error C2144: syntax error :
'<Unknown>' should be preceded by '<Unknown>'
c:\TEMP\TestBed\TestStruct\main.c(12) : error C2143: syntax error :
missing ';' before 'identifier'
c:\TEMP\TestBed\TestStruct\main.c(12) : error C2065: 'f' : undeclared
identifier
c:\TEMP\TestBed\TestStruct\main.c(12) : warning C4047: '=' : 'int'
differs in levels of indirection from 'void (__cdecl *)(MyStruct *)'
c:\TEMP\TestBed\TestStruct\main.c(13) : error C2063: 'f' : not a
function

Build log was saved at "file://c:\Temp\TestBed\TestStruct\Debug
\BuildLog.htm"
TestStruct - 7 error(s), 1 warning(s)

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

under Mingw (gcc 3.2.3) compiles fine.
 
G

Gavin Deane

ok ... I haven't done much C and tend to stick to C++. I picked up
this practice of nameing structs _xxxxx and then typedef _xxxxx xxxxxx
from Glib

You can drop the typedef completely in C++.

struct MyStruct
{
// stuff goes here...
};

is sufficient.

Gavin Deane
 
N

Nindi

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

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

struct _MyStruct;
typedef struct _MyStruct MyStruct;

typedef void (*funcType)(MyStruct *);

struct _MyStruct {double x;};

void MyFunc(MyStruct *theStruct){printf(" %f ",theStruct->x);}

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

}

............................................................................­....

Under gcc , no problem .

More and more inclined to think its a bug. Even more simplified

main1.c
..................................................................

struct MyStruct {double x;};

void MyFunc(){}

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

}

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


main2.c
..................................................................

struct MyStruct {double x;};

void MyFunc(){}

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

}

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


main3.c
..................................................................

struct MyStruct {double x;};

void MyFunc(){}

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

}

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



main1.c fails to compile under VS 2003 & 2005 , main2.c and main3.c
compile fine.

All compile under gcc fine.
 
A

Alf P. Steinbach

* Nindi:
...............................................................
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

.................................................................
Compiling...
main.c
c:\TEMP\TestBed\TestStruct\main.c(12) : error C2275: 'funcType' :
illegal use of this type as an expression

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.
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top