struct problem

B

Bern

The following is C code:

typedef struct __Haha Haha, *PHaha;

struct __Haha {

int i;

};

void main(){

Haha haha;

haha.i = 123;

PHaha phaha = & haha; // <<<<< error line

phaha->i = 234;

}

---------------------------------------------



i compiled the code above in C using MS compiler and it

generates an error in the line indicated.
The error is "illegal use of this type as an expression".
Yet when i compiled it as C++ code, everything is alright.
So whats the problem?
 
K

Karl Heinz Buchegger

Bern said:
The following is C code:

typedef struct __Haha Haha, *PHaha;

struct __Haha {

int i;

};

void main(){

Haha haha;

haha.i = 123;

PHaha phaha = & haha; // <<<<< error line

phaha->i = 234;

}

---------------------------------------------

i compiled the code above in C using MS compiler and it

generates an error in the line indicated.
The error is "illegal use of this type as an expression".
Yet when i compiled it as C++ code, everything is alright.
So whats the problem?

In C all declarations have to be at the beginning of the block.

int main() { // note that main returns int! Always!
Haha haha;
PHaha phaha = & haha;

haha.i = 123;
phaha->i = 234;
}
 
V

Victor Bazarov

Bern said:
The following is C code:

typedef struct __Haha Haha, *PHaha;

struct __Haha {

int i;

};

void main(){

Haha haha;

haha.i = 123;

PHaha phaha = & haha; // <<<<< error line

phaha->i = 234;

}

---------------------------------------------



i compiled the code above in C using MS compiler and it

generates an error in the line indicated.
The error is "illegal use of this type as an expression".
Yet when i compiled it as C++ code, everything is alright.
So whats the problem?

This is a C++ newsgroup. You compiled C++ and got no problem, right?
So, next time please go to comp.lang.c to ask what's wrong from the
C point of view.

The error is due to the fact that a declaration statement cannot appear
in a C block _after_ any executable statement. In C++ that requirement
has been removed. IOW, in C all declarations have to be placed at the
beginning of the block.

Oh, and neither language allows 'void main', BTW.

Victor
 
A

Andre Heinen

The following is C code:

<snip>

i compiled the code above in C using MS compiler and it
generates an error in the line indicated.
The error is "illegal use of this type as an expression".
Yet when i compiled it as C++ code, everything is alright.
So whats the problem?

This is off-topic here, as it is a C question, but try moving the
error line upwards: in C you must define your variables at the
beginning of the function:
int main(void){
Haha haha;
PHaha phaha = & haha;
haha.i = 123;
...

Also:
1) main() should return an int,
2) you'd better not use identifiers starting with an underscore.
In many cases they are reserved for the compiler, and likely to
get you into trouble.

HTH,
 
R

Rob Williscroft

Karl Heinz Buchegger wrote in in
comp.lang.c++:
In C all declarations have to be at the beginning of the block.

Its of topic (c.l.c++) but I just put this (test.c) through gcc 3.4 (*):

#include <stdio.h>

int main()
{
printf( "in main()\n" );

int i = 10;

printf( "after i = %d\n", i );
}

running: ...\gcc-3.4\bin\gcc -O1 test.c -o test-gcc34.exe
Test Pass
output:
in main()
after i = 10

I'm not sure when this was introduced in to C, but clearly
MSVC isn't upto the latest C standard.

*) Borland CBuildeX (preview) and gcc 3.2 also compiled,
MSVC 7.1 didn't.

Rob.
 
R

Rolf Magnus

Rob said:
Karl Heinz Buchegger wrote in in
comp.lang.c++:


Its of topic (c.l.c++) but I just put this (test.c) through gcc 3.4
(*):

#include <stdio.h>

int main()
{
printf( "in main()\n" );

int i = 10;

printf( "after i = %d\n", i );
}

running: ...\gcc-3.4\bin\gcc -O1 test.c -o test-gcc34.exe
Test Pass
output:
in main()
after i = 10

I'm not sure when this was introduced in to C,

1999, in ISO 9899-1999.
but clearly MSVC isn't upto the latest C standard.

Or maybe it isn't set to C99 mode.
 
D

Default User

Rob said:
Karl Heinz Buchegger wrote in in
comp.lang.c++:

Its of topic (c.l.c++) but I just put this (test.c) through gcc 3.4 (*):


I don't believe gcc implements the new C standard yet. However, the
feature under discussion has been available as an extension for some
time.



Brian Rodenborn
 
D

Default User

In C all declarations have to be at the beginning of the block.


That was true under the old standard, and was available from some
compilers as an extension before that. The new standard allows it, but
I'm not sure there are any compilers yet that implement the new standard
fully, obviously the one the OP is using still is C89.




Brian Rodenborn
 
J

John Carson

Rolf Magnus said:
1999, in ISO 9899-1999.


Or maybe it isn't set to C99 mode.


There is no such mode. MSVC doesn't support C99 and (as far as I know) has
given no indication of an intention to do so in the future.
 
R

Rolf Magnus

Default said:
I don't believe gcc implements the new C standard yet.

It does. You can switch it on with -std=c99, but I don't know how good
that support is.
Btw: that standard isn't so new really. It is already 5 years old and
has been available as a draft even longer.
 
D

Default User

Rolf said:
Default User wrote:

It does. You can switch it on with -std=c99, but I don't know how good
that support is.

Yeah, I couldn't remember if they had even nominal support implemented
yet.
Btw: that standard isn't so new really. It is already 5 years old and
has been available as a draft even longer.

Well, it's still the new standard, as opposed to the 10-year older one.



Brian Rodenborn
 
J

Jack Klein

The following is C code:

No it's not, not legal or correct C code, nor is it legal or correct
C++ code.
typedef struct __Haha Haha, *PHaha;

All identifiers beginning with two underscores, or with an underscore
followed by an uppercase letter, are reserved for the implementation
in both C and C++. It is not legal for you to define such symbols in
your program.
struct __Haha {

int i;

};

void main(){

"void main()" is not legal in either C or C++. Both languages require
that main() be defined with a return type of int.
Haha haha;

haha.i = 123;

PHaha phaha = & haha; // <<<<< error line

phaha->i = 234;

}

---------------------------------------------



i compiled the code above in C using MS compiler and it

generates an error in the line indicated.
The error is "illegal use of this type as an expression".
Yet when i compiled it as C++ code, everything is alright.
So whats the problem?

The problem is that you are writing code that is invalid in both
languages, and using a compiler that does not conform to either
language standard.
 

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
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top