Connecting Tech Pros Worldwide Forums | Help | Site Map

struct problem

Bern
Guest
 
Posts: n/a
#1: Jul 22 '05
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?






Karl Heinz Buchegger
Guest
 
Posts: n/a
#2: Jul 22 '05

re: struct problem


Bern wrote:[color=blue]
>
> 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?[/color]

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;
}

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Victor Bazarov
Guest
 
Posts: n/a
#3: Jul 22 '05

re: struct problem


Bern wrote:[color=blue]
> 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?[/color]

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
Andre Heinen
Guest
 
Posts: n/a
#4: Jul 22 '05

re: struct problem


On Thu, 12 Aug 2004 20:40:05 +0800, " Bern" <x@x.com> wrote:
[color=blue]
>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?[/color]

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,

--
Andre Heinen
My address is "a dot heinen at europeanlink dot com"
Rob Williscroft
Guest
 
Posts: n/a
#5: Jul 22 '05

re: struct problem


Karl Heinz Buchegger wrote in news:411B6FCE.19A507E4@gascad.at in
comp.lang.c++:
[color=blue]
> Bern wrote:[color=green]
>>
>> 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?[/color]
>
> In C all declarations have to be at the beginning of the block.
>[/color]

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.
--
http://www.victim-prime.dsl.pipex.com/
Rolf Magnus
Guest
 
Posts: n/a
#6: Jul 22 '05

re: struct problem


Rob Williscroft wrote:
[color=blue]
> Karl Heinz Buchegger wrote in news:411B6FCE.19A507E4@gascad.at in
> comp.lang.c++:
>[color=green]
>> Bern wrote:[color=darkred]
>>>
>>> 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?[/color]
>>
>> In C all declarations have to be at the beginning of the block.
>>[/color]
>
> 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,[/color]

1999, in ISO 9899-1999.
[color=blue]
> but clearly MSVC isn't upto the latest C standard.[/color]

Or maybe it isn't set to C99 mode.


John Carson
Guest
 
Posts: n/a
#7: Jul 22 '05

re: struct problem


"Rolf Magnus" <ramagnus@t-online.de> wrote in message
news:cfg4lu$aph$05$1@news.t-online.com[color=blue]
> Rob Williscroft wrote:
>[color=green]
>>
>> I'm not sure when this was introduced in to C,[/color]
>
> 1999, in ISO 9899-1999.
>[color=green]
>> but clearly MSVC isn't upto the latest C standard.[/color]
>
> Or maybe it isn't set to C99 mode.[/color]


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.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Default User
Guest
 
Posts: n/a
#8: Jul 22 '05

re: struct problem


Rob Williscroft wrote:[color=blue]
>
> Karl Heinz Buchegger wrote in news:411B6FCE.19A507E4@gascad.at in
> comp.lang.c++:[/color]
[color=blue][color=green]
> > In C all declarations have to be at the beginning of the block.
> >[/color]
>
> Its of topic (c.l.c++) but I just put this (test.c) through gcc 3.4 (*):[/color]


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
Default User
Guest
 
Posts: n/a
#9: Jul 22 '05

re: struct problem


Karl Heinz Buchegger wrote:[color=blue]
>[/color]
[color=blue]
> In C all declarations have to be at the beginning of the block.[/color]


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
Rolf Magnus
Guest
 
Posts: n/a
#10: Jul 22 '05

re: struct problem


Default User wrote:
[color=blue]
> Rob Williscroft wrote:[color=green]
>>
>> Karl Heinz Buchegger wrote in news:411B6FCE.19A507E4@gascad.at in
>> comp.lang.c++:[/color]
>[color=green][color=darkred]
>> > In C all declarations have to be at the beginning of the block.
>> >[/color]
>>
>> Its of topic (c.l.c++) but I just put this (test.c) through gcc 3.4
>> (*):[/color]
>
>
> I don't believe gcc implements the new C standard yet.[/color]

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.



Default User
Guest
 
Posts: n/a
#11: Jul 22 '05

re: struct problem


Rolf Magnus wrote:[color=blue]
>
> Default User wrote:[/color]
[color=blue][color=green]
> > I don't believe gcc implements the new C standard yet.[/color]
>
> It does. You can switch it on with -std=c99, but I don't know how good
> that support is.[/color]

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

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



Brian Rodenborn
Jack Klein
Guest
 
Posts: n/a
#12: Jul 22 '05

re: struct problem


On Thu, 12 Aug 2004 20:40:05 +0800, " Bern" <x@x.com> wrote in
comp.lang.c++:
[color=blue]
> The following is C code:[/color]

No it's not, not legal or correct C code, nor is it legal or correct
C++ code.
[color=blue]
> typedef struct __Haha Haha, *PHaha;[/color]

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.
[color=blue]
> struct __Haha {
>
> int i;
>
> };
>
> void main(){[/color]

"void main()" is not legal in either C or C++. Both languages require
that main() be defined with a return type of int.
[color=blue]
> 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?[/color]

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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Closed Thread


Similar C / C++ bytes