Thread: operator assoiciation

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    12

    operator assoiciation

    why sizeof (int)(*p) is not compilable by gcc?
    I thought sizeof, (int) and * has the same order of precedence and the association should be right to left, but
    sizeof (int)(*p) is wrong.
    I have to use sizeof ((int)(*p)) instead.

    Also, sizeof (int) *p is interpreted as (sizeof(int) )* p instead of sizeof ((int)(*p)) as I expected. Any reason?

    Thanks for any tips

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    sizeof x is an operator that produces a numeric value by your precompiler. Ergo, sizeof(int)(*p) is like saying 4(*p). Why is 4(*p) not compileable by gcc?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well do you want sizeof(int) or sizeof(*p) ?

    Because your first efforts didn't make a lot of sense.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Incidentally, why would you want to use a cast inside a sizeof operator anyway? Perhaps you want sizeof(int) * sizeof(*p);

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    Quote Originally Posted by master5001 View Post
    sizeof x is an operator that produces a numeric value by your precompiler. Ergo, sizeof(int)(*p) is like saying 4(*p). Why is 4(*p) not compileable by gcc?
    sizeof is not handled by precompiler and it is an operator with the same order of precedence as (typecast) and * (dereference)

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Interesting, though yet despite your knowledge of which processes are pre-compiler duties and which are compiler duties how is it you fail to see the flaw in your own code?

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    Quote Originally Posted by Salem View Post
    Well do you want sizeof(int) or sizeof(*p) ?

    Because your first efforts didn't make a lot of sense.
    I have programmed C for 5+ years and I do know how to get sizeof(int) and how to get sizeof(*p).
    I will never write a code like what I posted.
    I am discussing the problem from the "theoretical" POV.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Thanks for any tips
    Ok, so I gave my two cents from my decade+ years of programming.

    I will never write a code like what I posted.
    Then why are you thanking us? Keep up the good work not writing code like that!

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    Quote Originally Posted by master5001 View Post
    Ok, so I gave my two cents from my decade+ years of programming.



    Then why are you thanking us? Keep up the good work not writing code like that!
    Thanks for your help!
    But seriously, I still have not got the answer for my questions yet.
    I would appreciate any further help.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Seriously the reason is just order of operations. I follow and agree with gcc on this one.

    sizeof (int)(*p)

    sizeof (int)(*p)
    sizeof (int)(*p)

    thus

    4(*p)

    which shouldn't compile.

    You are wanting order of operations to look at it like this

    sizeof (int)(*p)
    sizeof (int)(*p)

    But in order to kill that ambiguity, one should use parenthesis. I would surmise this is not consistent behavior and should actually go on the "undefined" list. Since some compilers may not agree with my order of operations nor gcc's.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think your answer is greedy parsing, sizeof is going to take the next thing it can find that makes sense -- (int) makes sense, because sizeof can take a parenthesized type, but (int)(*p) doesn't. Hence sizeof(int) is parsed.

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    Quote Originally Posted by master5001 View Post
    Seriously the reason is just order of operations. I follow and agree with gcc on this one.

    sizeof (int)(*p)

    sizeof (int)(*p)
    sizeof (int)(*p)

    thus

    4(*p)

    which shouldn't compile.
    Why sizeof is associated before (int)?
    This should be right to left, is it?

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Which is why I would contend this is a case that is most likely undefined. But I would need CornedBee's two cents on this one. sizeof is not particularly intelligent as far as what it takes as an argument. It just grabs its nearest neighbor.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by tabstop View Post
    I think your answer is greedy parsing, sizeof is going to take the next thing it can find that makes sense -- (int) makes sense, because sizeof can take a parenthesized type, but (int)(*p) doesn't. Hence sizeof(int) is parsed.
    Thusly, parentheses in your example is required so that sizeof returns the size of the result of the correct expression.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    Quote Originally Posted by master5001 View Post
    sizeof is not particularly intelligent as far as what it takes as an argument. It just grabs its nearest neighbor.
    This makes sense to me: though it is defined as the same order of precedence as (int) and * but not implemented in that way in GNU. How about other C compiler, like intel's one?

Popular pages Recent additions subscribe to a feed

Tags for this Thread