What does the expression "sizeof(int)*p" mean?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roy Yao

    What does the expression "sizeof(int)*p" mean?

    Does it mean "(sizeof(in t))* (p)" or "sizeof( (int)(*p) )" ?

    According to my analysis, operator sizeof, (type) and * have the same
    precedence, and they combine from right to left. Then this expression should
    equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why?

    Can anyone help me? Thanks.

    Best regards.
    Roy






  • Y. Keerthi Sagar

    #2
    Re: What does the expression "sizeof(in t)*p" mean?

    "Roy Yao" <flyao@263.ne t> wrote in message news:<bk37ib$2r m6$1@mail.cn99. com>...[color=blue]
    > Does it mean "(sizeof(in t))* (p)" or "sizeof( (int)(*p) )" ?
    >
    > According to my analysis, operator sizeof, (type) and * have the same
    > precedence, and they combine from right to left. Then this expression should
    > equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why?
    >
    > Can anyone help me? Thanks.
    >
    > Best regards.
    > Roy[/color]




    Hey,
    Both "(sizeof(in t))* (p)" or "sizeof( (int)(*p) )" are not same.
    "(sizeof(in t))* (p)" means you are multiplying sizeof(int), i.e. 2
    (bytes), with the value of "p". Whereas "sizeof( (int)(*p) )" means,
    First you are getting the value of at the adderess p (because *p gives
    the value of another variable), then you are converting the returned
    value into interger format and finally you are detecting the size of
    that result. If you have any doubts contact at my ID
    keethisagar_y@y ahoo.co.in.

    Be Cool,
    Y. Keerthi
    Sagar & Ashok.

    Comment

    • Roy Yao

      #3
      Re: What does the expression &quot;sizeof(in t)*p&quot; mean?

      Hello Keerthi,

      Thanks for your enthusiasm.

      My email to you was rejected by your mail server, so I reply you here.

      In this expression, what confused me is how the compilers (or ANSI C)
      determine the combination of operator sizeof, (type) and *.

      In my opion it should equal to expression "sizeof( (int)(*p) )". The
      reason is that operator sizeof, (type) and * have the same
      precedence, and they combine from right to left. But the compilers prefer
      "(sizeof(in t))* (p)".

      Now can you give me a convictive explain from the precedence and
      combination order of operator ?

      Thanks again.

      Best regards,
      Roy Yao


      Comment

      • Josh Sebastian

        #4
        Re: What does the expression &quot;sizeof(in t)*p&quot; mean?

        On Tue, 16 Sep 2003 08:44:02 +0800, Roy Yao wrote:
        [color=blue]
        > Hello Keerthi,
        >
        > Thanks for your enthusiasm.
        >
        > My email to you was rejected by your mail server, so I reply you here.
        >
        > In this expression, what confused me is how the compilers (or ANSI C)
        > determine the combination of operator sizeof, (type) and *.
        >
        > In my opion it should equal to expression "sizeof( (int)(*p) )". The
        > reason is that operator sizeof, (type) and * have the same
        > precedence, and they combine from right to left. But the compilers prefer
        > "(sizeof(in t))* (p)".
        >
        > Now can you give me a convictive explain from the precedence and
        > combination order of operator ?[/color]

        I told you in my other post, that's NOT the (type) operator! (type) is the
        /operand/ of sizeof.

        Josh

        Comment

        • wang tianxing

          #5
          Re: What does the expression &quot;sizeof(in t)*p&quot; mean?

          On Mon, 15 Sep 2003 22:00:09 -0400, Josh Sebastian <curien@cox.net >
          wrote:

          : On Tue, 16 Sep 2003 08:44:02 +0800, Roy Yao wrote:
          :
          : > Hello Keerthi,
          : >
          : > Thanks for your enthusiasm.
          : >
          : > My email to you was rejected by your mail server, so I reply you here.

          You(Roy) are supposed to reply here.

          : > In this expression, what confused me is how the compilers (or ANSI C)
          : > determine the combination of operator sizeof, (type) and *.
          : >
          : > In my opion it should equal to expression "sizeof( (int)(*p) )". The
          : > reason is that operator sizeof, (type) and * have the same
          : > precedence, and they combine from right to left. But the compilers prefer
          : > "(sizeof(in t))* (p)".
          : >
          : > Now can you give me a convictive explain from the precedence and
          : > combination order of operator ?
          :
          : I told you in my other post, that's NOT the (type) operator! (type) is the
          : /operand/ of sizeof.

          You are right, but he wants to know why. :)

          Some relevent production rules:

          unary-expression:
          postfix-expression
          ++ cast-expression
          -- cast-expression
          unary-operator cast-expression
          sizeof unary-expression
          sizeof ( type-id )
          new-expression
          delete-expression

          unary-operator: one of
          * & + - ! ~

          The expression in question:

          sizeof (int) * p

          '(int) * p' isn't a unary-expression, because it is not starting
          with ++, --, *, &, +, -, !, ~, sizeof, new, or delete, and it is
          not a postfix-expression (believe me!). So a compiler can't parse
          the expression in question as 'sizeof unary-expression'.


          Regards,
          tx

          Comment

          • jeffc

            #6
            Re: What does the expression &quot;sizeof(in t)*p&quot; mean?


            "Roy Yao" <flyao@263.ne t> wrote in message
            news:bk5mbd$248 l$1@mail.cn99.c om...[color=blue]
            > Hello Keerthi,
            >
            > Thanks for your enthusiasm.
            >
            > My email to you was rejected by your mail server, so I reply you here.
            >
            > In this expression, what confused me is how the compilers (or ANSI C)
            > determine the combination of operator sizeof, (type) and *.
            >
            > In my opion it should equal to expression "sizeof( (int)(*p) )". The
            > reason is that operator sizeof, (type) and * have the same
            > precedence, and they combine from right to left. But the compilers prefer
            > "(sizeof(in t))* (p)".
            >
            > Now can you give me a convictive explain from the precedence and
            > combination order of operator ?[/color]

            sizeof is a function, and the parameter it takes is whatever is inside the
            parens. If you wrote
            sizeof((int)*p) , you'd evaluate the expression inside the outer parens
            first. You cast whatever
            *p is to an int, then take the size of that. Same as sizeof(int). But what
            you wrote in your subject
            is not exactly the same as either of the 2 options in your text message.


            Comment

            • Kevin Goodsell

              #7
              Re: What does the expression &quot;sizeof(in t)*p&quot; mean?

              jeffc wrote:
              [color=blue]
              > sizeof is a function,[/color]

              No, it's not. It is an operator.
              [color=blue]
              > and the parameter it takes is whatever is inside the
              > parens. If you wrote
              > sizeof((int)*p) , you'd evaluate the expression inside the outer parens
              > first.[/color]

              No, sizeof's operand is not evaluated.

              -Kevin
              --
              My email address is valid, but changes periodically.
              To contact me please use the address from a recent posting.

              Comment

              • Ron Natalie

                #8
                Re: What does the expression &quot;sizeof(in t)*p&quot; mean?


                "jeffc" <nobody@nowhere .com> wrote in message news:3f6b50aa_3 @news1.prserv.n et...
                [color=blue]
                > sizeof is a function,[/color]

                sizeof is not a function. It's an operator.
                [color=blue]
                > and the parameter it takes is whatever is inside the
                > parens.[/color]

                Sorry untrue. The parens aren't strictly requird. The grammar of sizeof
                is:

                sizeof unary-expression // note no parens here.
                swizeof ( type-id )
                [color=blue]
                > you'd evaluate the expression inside the outer parens
                > first.[/color]

                Parens have no bearing on order of evaulation in C++.


                Comment

                • Xenos

                  #9
                  Re: What does the expression &quot;sizeof(in t)*p&quot; mean?


                  "jeffc" <nobody@nowhere .com> wrote in message
                  news:3f6b50aa_3 @news1.prserv.n et...[color=blue]
                  >
                  > "Roy Yao" <flyao@263.ne t> wrote in message
                  > news:bk5mbd$248 l$1@mail.cn99.c om...[color=green]
                  > > Hello Keerthi,
                  > >
                  > > Thanks for your enthusiasm.
                  > >
                  > > My email to you was rejected by your mail server, so I reply you here.
                  > >
                  > > In this expression, what confused me is how the compilers (or ANSI C)
                  > > determine the combination of operator sizeof, (type) and *.
                  > >
                  > > In my opion it should equal to expression "sizeof( (int)(*p) )". The
                  > > reason is that operator sizeof, (type) and * have the same
                  > > precedence, and they combine from right to left. But the compilers[/color][/color]
                  prefer[color=blue][color=green]
                  > > "(sizeof(in t))* (p)".
                  > >
                  > > Now can you give me a convictive explain from the precedence and
                  > > combination order of operator ?[/color]
                  >
                  > sizeof is a function, and the parameter it takes is whatever is inside the
                  > parens. If you wrote
                  > sizeof((int)*p) , you'd evaluate the expression inside the outer parens
                  > first. You cast whatever
                  > *p is to an int, then take the size of that. Same as sizeof(int). But[/color]
                  what[color=blue]
                  > you wrote in your subject
                  > is not exactly the same as either of the 2 options in your text message.
                  >
                  >[/color]

                  Additionally, his second option: (sizeof(int)*(p )) is exactly the same as
                  the one in his subject line.


                  DrX.


                  Comment

                  • Xenos

                    #10
                    Re: What does the expression &quot;sizeof(in t)*p&quot; mean?


                    "jeffc" <nobody@nowhere .com> wrote in message
                    news:3f6b50aa_3 @news1.prserv.n et...[color=blue]
                    >
                    > sizeof is a function, and the parameter it takes is whatever is inside the
                    > parens. If you wrote
                    > sizeof((int)*p) , you'd evaluate the expression inside the outer parens
                    > first. You cast whatever
                    > *p is to an int, then take the size of that. Same as sizeof(int). But[/color]
                    what[color=blue]
                    > you wrote in your subject
                    > is not exactly the same as either of the 2 options in your text message.
                    >
                    >[/color]

                    No, sizeof is an operator. The paranteses are only required for uses with a
                    type expression. Any other use only need them because of precedence.
                    Examples are:

                    sizeof(int)
                    sizeof 12345
                    sizeof 'a'
                    sizeof (a + b)

                    DrX.


                    Comment

                    • jeffc

                      #11
                      Re: What does the expression &quot;sizeof(in t)*p&quot; mean?


                      "Xenos" <dont.spam.me@s pamhate.com> wrote in message
                      news:bkfkti$ke3 3@cui1.lmms.lmc o.com...[color=blue]
                      >
                      > No, sizeof is an operator. The paranteses are only required for uses with[/color]
                      a[color=blue]
                      > type expression.[/color]

                      Makes no difference.


                      Comment

                      • jeffc

                        #12
                        Re: What does the expression &quot;sizeof(in t)*p&quot; mean?


                        "Xenos" <dont.spam.me@s pamhate.com> wrote in message
                        news:bkfl2e$ke3 4@cui1.lmms.lmc o.com...[color=blue][color=green]
                        > > But[/color]
                        > what[color=green]
                        > > you wrote in your subject
                        > > is not exactly the same as either of the 2 options in your text message.[/color]
                        >
                        > Additionally, his second option: (sizeof(int)*(p )) is exactly the same[/color]
                        as[color=blue]
                        > the one in his subject line.[/color]

                        True.


                        Comment

                        • WW

                          #13
                          Re: What does the expression &quot;sizeof(in t)*p&quot; mean?

                          jeffc wrote:[color=blue]
                          > "Xenos" <dont.spam.me@s pamhate.com> wrote in message
                          > news:bkfkti$ke3 3@cui1.lmms.lmc o.com...[color=green]
                          >>
                          >> No, sizeof is an operator. The paranteses are only required for
                          >> uses with a type expression.[/color]
                          >
                          > Makes no difference.[/color]

                          You stated that sizeof is a function. It is not. It is an operator. Makes
                          all the difference possible. It is an operator, same way as the + sign ot
                          the () function call operator.

                          --
                          WW aka Attila


                          Comment

                          • jeffc

                            #14
                            Re: What does the expression &quot;sizeof(in t)*p&quot; mean?


                            "Ron Natalie" <ron@sensor.com > wrote in message
                            news:3f6b57c0$0 $198$9a6e19ea@n ews.newshosting .com...[color=blue]
                            >
                            > "jeffc" <nobody@nowhere .com> wrote in message[/color]
                            news:3f6b50aa_3 @news1.prserv.n et...[color=blue]
                            >[color=green]
                            > > sizeof is a function,[/color]
                            >
                            > sizeof is not a function. It's an operator.[/color]

                            Same thing.
                            [color=blue][color=green]
                            > > and the parameter it takes is whatever is inside the
                            > > parens.[/color]
                            >
                            > Sorry untrue. The parens aren't strictly requird.[/color]

                            I didn't say they were required. I said it takes whatever is inside the
                            parens. Are you arguing with the term "parameter" ? I could easily argue
                            that all operators take parameters (or arguments). This is more clear in
                            C++ than C.
                            [color=blue]
                            > Parens have no bearing on order of evaulation in C++.[/color]

                            Of course they do. Compare:
                            1 + (2 * 3)
                            (1 + 2) * 3

                            But that's beside the point. What I wrote was "If you wrote
                            sizeof((int)*p) , you'd evaluate the expression inside the outer parens
                            first." That is true. Let's say p were of type double*. We are taking the
                            size of an int, not double, and not int multiplied by double.


                            Comment

                            • jeffc

                              #15
                              Re: What does the expression &quot;sizeof(in t)*p&quot; mean?


                              "WW" <wolof@freemail .hu> wrote in message
                              news:bkfrfp$4pd $1@phys-news1.kolumbus. fi...[color=blue]
                              > jeffc wrote:[color=green]
                              > > "Xenos" <dont.spam.me@s pamhate.com> wrote in message
                              > > news:bkfkti$ke3 3@cui1.lmms.lmc o.com...[color=darkred]
                              > >>
                              > >> No, sizeof is an operator. The paranteses are only required for
                              > >> uses with a type expression.[/color]
                              > >
                              > > Makes no difference.[/color]
                              >
                              > You stated that sizeof is a function. It is not. It is an operator.[/color]
                              Makes[color=blue]
                              > all the difference possible. It is an operator, same way as the + sign ot
                              > the () function call operator.[/color]

                              Operators are functions. It's just that the syntax is sometimes a little
                              different. It sure does not make "all the difference possible". Why do you
                              think the term "Operator Functions" exists?


                              Comment

                              Working...