public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/15240] New: sizeof() doesn't work on struct fields
@ 2004-05-01 11:32 d dot yu dot bolkhovityanov at inp dot nsk dot su
  2004-05-01 11:51 ` [Bug c/15240] " falk at debian dot org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: d dot yu dot bolkhovityanov at inp dot nsk dot su @ 2004-05-01 11:32 UTC (permalink / raw)
  To: gcc-bugs

The sizeof operator fails to operate on struct fields, when they are specified
as "type.field".

Consider the following code:

---- BEGIN s.c ----
#include <stdio.h>

typedef struct {int x; int y;} s_t;

int main(void)
{
    printf("s=%d\n", sizeof(s_t.x));

    return 0;
}
---- END s.c ----

Gcc produces an error:

s.c: In function `main':
s.c:7: parse error before `.'

while applying sizeof() to a variable (e.g. "s_t v;" =>  "sizeof(v.x)") works
fine.  

This constraint seems absolutely strange -- there's no reason why sizeof of a
type's field can't be taken.

Of course, this problem can be overcome with a macro like
#define T_SIZEOF(type,field) sizeof(((type *)NULL)->field)
but this is weird...

-- 
           Summary: sizeof() doesn't work on struct fields
           Product: gcc
           Version: 2.96 (redhat)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: d dot yu dot bolkhovityanov at inp dot nsk dot su
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15240


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/15240] sizeof() doesn't work on struct fields
  2004-05-01 11:32 [Bug c/15240] New: sizeof() doesn't work on struct fields d dot yu dot bolkhovityanov at inp dot nsk dot su
@ 2004-05-01 11:51 ` falk at debian dot org
  2004-05-01 12:03 ` d dot yu dot bolkhovityanov at inp dot nsk dot su
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: falk at debian dot org @ 2004-05-01 11:51 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From falk at debian dot org  2004-05-01 11:51 -------
sizeof works for expressions, or for parenthized types. "(s_t.x)" is neither,
so it's not supposed to work.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15240


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/15240] sizeof() doesn't work on struct fields
  2004-05-01 11:32 [Bug c/15240] New: sizeof() doesn't work on struct fields d dot yu dot bolkhovityanov at inp dot nsk dot su
  2004-05-01 11:51 ` [Bug c/15240] " falk at debian dot org
@ 2004-05-01 12:03 ` d dot yu dot bolkhovityanov at inp dot nsk dot su
  2004-05-01 12:06 ` falk at debian dot org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: d dot yu dot bolkhovityanov at inp dot nsk dot su @ 2004-05-01 12:03 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From d dot yu dot bolkhovityanov at inp dot nsk dot su  2004-05-01 12:03 -------
So, to paraphrase your answer, gcc does what the standard dictates, and it's the
C standard which is in fault, right?

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15240


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/15240] sizeof() doesn't work on struct fields
  2004-05-01 11:32 [Bug c/15240] New: sizeof() doesn't work on struct fields d dot yu dot bolkhovityanov at inp dot nsk dot su
  2004-05-01 11:51 ` [Bug c/15240] " falk at debian dot org
  2004-05-01 12:03 ` d dot yu dot bolkhovityanov at inp dot nsk dot su
@ 2004-05-01 12:06 ` falk at debian dot org
  2004-05-01 12:25 ` schwab at suse dot de
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: falk at debian dot org @ 2004-05-01 12:06 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From falk at debian dot org  2004-05-01 12:06 -------
(In reply to comment #2)
> So, to paraphrase your answer, gcc does what the standard dictates, and it's the
> C standard which is in fault, right?

Yes.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15240


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/15240] sizeof() doesn't work on struct fields
  2004-05-01 11:32 [Bug c/15240] New: sizeof() doesn't work on struct fields d dot yu dot bolkhovityanov at inp dot nsk dot su
                   ` (2 preceding siblings ...)
  2004-05-01 12:06 ` falk at debian dot org
@ 2004-05-01 12:25 ` schwab at suse dot de
  2004-05-02  7:39 ` d dot yu dot bolkhovityanov at inp dot nsk dot su
  2004-05-03  3:40 ` bangerth at dealii dot org
  5 siblings, 0 replies; 7+ messages in thread
From: schwab at suse dot de @ 2004-05-01 12:25 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From schwab at suse dot de  2004-05-01 12:25 -------
(In reply to comment #2) 
> So, to paraphrase your answer, gcc does what the standard dictates, 
 
Yes. 
 
> and it's the C standard which is in fault, right? 
 
No. 
 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15240


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/15240] sizeof() doesn't work on struct fields
  2004-05-01 11:32 [Bug c/15240] New: sizeof() doesn't work on struct fields d dot yu dot bolkhovityanov at inp dot nsk dot su
                   ` (3 preceding siblings ...)
  2004-05-01 12:25 ` schwab at suse dot de
@ 2004-05-02  7:39 ` d dot yu dot bolkhovityanov at inp dot nsk dot su
  2004-05-03  3:40 ` bangerth at dealii dot org
  5 siblings, 0 replies; 7+ messages in thread
From: d dot yu dot bolkhovityanov at inp dot nsk dot su @ 2004-05-02  7:39 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From d dot yu dot bolkhovityanov at inp dot nsk dot su  2004-05-02 07:39 -------
> > and it's the C standard which is in fault, right? 
>  
> No. 

Andreas, sorry for taking your time, but can you please point me to some C
standard docs (discussion?) stating WHY such a reasonable behaviour is not allowed?

(The only constraint in the C99 standard (6.5.3.4) is "The sizeof operator shall
not be applied to an expression that has function type or an incomplete type, to
the parenthesized name of such a type, or to an expression that designates a
bit-field member."  But "struct_type_name.field_name" falls outside of that
constraint.)


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15240


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/15240] sizeof() doesn't work on struct fields
  2004-05-01 11:32 [Bug c/15240] New: sizeof() doesn't work on struct fields d dot yu dot bolkhovityanov at inp dot nsk dot su
                   ` (4 preceding siblings ...)
  2004-05-02  7:39 ` d dot yu dot bolkhovityanov at inp dot nsk dot su
@ 2004-05-03  3:40 ` bangerth at dealii dot org
  5 siblings, 0 replies; 7+ messages in thread
From: bangerth at dealii dot org @ 2004-05-03  3:40 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-05-03 03:40 -------
One possible answer is that contrary to Java, in C and C++ you cannot 
access members of _types_ using type.member. If anything, then this should 
be type::member (in C++), which would fail in your case since the 
member is non-static, though. The dot-operator can only be used to 
access members of _variables_, not _types_. 
 
W. 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15240


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-05-03  3:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-01 11:32 [Bug c/15240] New: sizeof() doesn't work on struct fields d dot yu dot bolkhovityanov at inp dot nsk dot su
2004-05-01 11:51 ` [Bug c/15240] " falk at debian dot org
2004-05-01 12:03 ` d dot yu dot bolkhovityanov at inp dot nsk dot su
2004-05-01 12:06 ` falk at debian dot org
2004-05-01 12:25 ` schwab at suse dot de
2004-05-02  7:39 ` d dot yu dot bolkhovityanov at inp dot nsk dot su
2004-05-03  3:40 ` bangerth at dealii dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).