C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why won't the compiler tell

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Mark Rance
Guest





PostPosted: Wed Apr 28, 2004 7:28 pm    Post subject: Why won't the compiler tell Reply with quote



Consider this:

typedef struct
{
char a[10];
char b[5];
char c[3];
} foo;

Why can I not execute a statement like:

size_t x = sizeof(foo.b); ??

The only way this works is if I have declared a variable of type foo...yet
the compiler knows quite well what the answer to what I am asking.

This just seems like something the compilers should be able to handle.

-Mark



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ralf
Guest





PostPosted: Thu Apr 29, 2004 11:35 am    Post subject: Re: Why won't the compiler tell Reply with quote



The sizeof operator needs either a type or a variable. What you gave it is
nothing of the two possibilities.

www.oop-trainer.de



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Niels Dybdahl
Guest





PostPosted: Thu Apr 29, 2004 9:24 pm    Post subject: Re: Why won't the compiler tell Reply with quote



Quote:
typedef struct
{
char a[10];
char b[5];
char c[3];
} foo;

Why can I not execute a statement like:

size_t x = sizeof(foo.b); ??

Some compilers can do that (MS VC).

Niels Dybdahl



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
llewelly
Guest





PostPosted: Thu Apr 29, 2004 9:37 pm    Post subject: Re: Why won't the compiler tell Reply with quote

"Mark Rance" <mrr (AT) pcisys (DOT) network> writes:

Quote:
Consider this:

typedef struct
{
char a[10];
char b[5];
char c[3];
} foo;

Why can I not execute a statement like:

size_t x = sizeof(foo.b); ??
[snip]


The operand of sizeof must be an expression. See 5.3.3 . 'foo.b' is
not a well-formed expression, since 'foo' is a type, not an
object.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Glen Low
Guest





PostPosted: Fri Apr 30, 2004 11:14 am    Post subject: Re: Why won't the compiler tell Reply with quote

Quote:
Consider this:

typedef struct
{
char a[10];
char b[5];
char c[3];
} foo;

Why can I not execute a statement like:

size_t x = sizeof(foo.b); ??

If you had done

struct foo
{
char a [10];
char b [5];
char c [3];
};

then you can simply do

size_t x = sizeof (foo::b);

Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Paul D. DeRocco
Guest





PostPosted: Fri Apr 30, 2004 11:21 am    Post subject: Re: Why won't the compiler tell Reply with quote

Quote:
"Mark Rance" <mrr (AT) pcisys (DOT) network> wrote

Consider this:

typedef struct
{
char a[10];
char b[5];
char c[3];
} foo;

Why can I not execute a statement like:

size_t x = sizeof(foo.b); ??

The only way this works is if I have declared a variable of type foo...yet
the compiler knows quite well what the answer to what I am asking.

This just seems like something the compilers should be able to handle.

I would think that the correct syntax would be sizeof(foo::b), but that's
illegal, too. However, you can kluge it with:

size_t x = sizeof(((foo*)0)->b);

--

Ciao, Paul D. DeRocco
Paul mailto:pderocco (AT) ix (DOT) netcom.com


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Mark Rance
Guest





PostPosted: Fri Apr 30, 2004 11:35 am    Post subject: Re: Why won't the compiler tell Reply with quote


"Niels Dybdahl" <ndy (AT) fjern (DOT) detteesko-graphics.com> wrote

Quote:
typedef struct
{
char a[10];
char b[5];
char c[3];
} foo;

Why can I not execute a statement like:

size_t x = sizeof(foo.b); ??

Some compilers can do that (MS VC).

Not true. I am using the VC7 compiler and it does not work.
This does, however, size_t x = (((foo *) 0)->b);


-Mark



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Mark Rance
Guest





PostPosted: Fri Apr 30, 2004 11:35 am    Post subject: Re: Why won't the compiler tell Reply with quote


"llewelly" <llewelly.at (AT) xmission (DOT) dot.com> wrote

Quote:
"Mark Rance" <mrr (AT) pcisys (DOT) network> writes:

Consider this:

typedef struct
{
char a[10];
char b[5];
char c[3];
} foo;

Why can I not execute a statement like:

size_t x = sizeof(foo.b); ??
[snip]

The operand of sizeof must be an expression. See 5.3.3 . 'foo.b' is
not a well-formed expression, since 'foo' is a type, not an
object.

I know, but it just seemed odd since the compiler DOES know the answer...I
thought I should be able to get away with it. I posted the work-around I
used in an earlier post to this thread, if interested.

-Mark



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sat May 01, 2004 3:09 am    Post subject: Re: Why won't the compiler tell Reply with quote

In message <aBnkc.41$fh4.11 (AT) newsread1 (DOT) news.pas.earthlink.net>, Paul D.
DeRocco <pderocco (AT) ix (DOT) netcom.com> writes
Quote:
I would think that the correct syntax would be sizeof(foo::b), but that's
illegal, too. However, you can kluge it with:

size_t x = sizeof(((foo*)0)->b);

I think that is the one case where (foo *)0 is OK because sizeof does
not evaluate the expression. But what puzzles me is why you would ever
want to do this because you already know what b's type is and so can
simply ask for the sizeof the type.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon May 03, 2004 2:51 pm    Post subject: Re: Why won't the compiler tell Reply with quote

"Mark Rance" <mrr (AT) pcisys (DOT) network> wrote

Quote:
"Niels Dybdahl" <ndy (AT) fjern (DOT) detteesko-graphics.com> wrote in message
news:4090b42b$0$164$edfadb0f (AT) dtext02 (DOT) news.tele.dk...
typedef struct
{
char a[10];
char b[5];
char c[3];
} foo;

Why can I not execute a statement like:

size_t x = sizeof(foo.b); ??

Some compilers can do that (MS VC).

Not true. I am using the VC7 compiler and it does not work.
This does, however, size_t x = (((foo *) 0)->b);

You mean:

size_t x = sizeof( ((foo*)0)->b ) ;

don't you. (What you have written should not compile.)

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon May 03, 2004 2:51 pm    Post subject: Re: Why won't the compiler tell Reply with quote

Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote

Quote:
In message <aBnkc.41$fh4.11 (AT) newsread1 (DOT) news.pas.earthlink.net>, Paul D.
DeRocco <pderocco (AT) ix (DOT) netcom.com> writes

I would think that the correct syntax would be sizeof(foo::b), but that's
illegal, too. However, you can kluge it with:

size_t x = sizeof(((foo*)0)->b);

I think that is the one case where (foo *)0 is OK because sizeof does
not evaluate the expression. But what puzzles me is why you would ever
want to do this because you already know what b's type is and so can
simply ask for the sizeof the type.

In the case in question, b's type was char[5], or something like that.
You don't want to have the magic number appearing in more than one
place.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Michiel Salters
Guest





PostPosted: Tue May 04, 2004 10:37 am    Post subject: Re: Why won't the compiler tell Reply with quote

"Mark Rance" <mrr (AT) pcisys (DOT) network> wrote

Quote:
Consider this:

typedef struct
{
char a[10];
char b[5];
char c[3];
} foo;

Why can I not execute a statement like:

size_t x = sizeof(foo.b); ??

The only way this works is if I have declared a variable of type foo...yet
the compiler knows quite well what the answer to what I am asking.

This just seems like something the compilers should be able to handle.

In addition to what Francis noted, in C++ we'd probably write

struct foo {
typedef char a_type[10];
typedef char b_type[5];
typedef char c_type[3];
a_type a;
b_type b;
c_type c;
};
size_t x = sizeof( foo::a_type );


Regards,
Michiel Salters

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Balog Pal
Guest





PostPosted: Sun May 09, 2004 5:59 pm    Post subject: Re: Why won't the compiler tell Reply with quote

"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote


Quote:
size_t x = sizeof(((foo*)0)->b);

I think that is the one case where (foo *)0 is OK because sizeof does
not evaluate the expression. But what puzzles me is why you would ever
want to do this because you already know what b's type is and so can
simply ask for the sizeof the type.

That would introduce unwanted redundancy.
The whole thing is a good example for why we miss typeof() so much.

In a solid program I could change type of foo::b at that only location in
the class definition, and everything keep working.

The suggestion to use typedefs from the other post is pretty ugly if that
typedef doeesn't make sense in itself, what is a frequent case.

Paul



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.