473,732 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


Jul 19 '05 #1
70 8876
"Roy Yao" <fl***@263.ne t> wrote in message news:<bk******* ****@mail.cn99. com>...
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



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
ke***********@y ahoo.co.in.

Be Cool,
Y. Keerthi
Sagar & Ashok.
Jul 19 '05 #2
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
Jul 19 '05 #3
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.

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.

Josh
Jul 19 '05 #4
On Mon, 15 Sep 2003 22:00:09 -0400, Josh Sebastian <cu****@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
Jul 19 '05 #5

"Roy Yao" <fl***@263.ne t> wrote in message
news:bk******** ***@mail.cn99.c om...
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 ?


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.
Jul 19 '05 #6
jeffc wrote:
sizeof is a function,
No, it's not. It is an operator.
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.


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.

Jul 19 '05 #7

"jeffc" <no****@nowhere .com> wrote in message news:3f******** @news1.prserv.n et...
sizeof is a function,
sizeof is not a function. It's an operator.
and the parameter it takes is whatever is inside the
parens.
Sorry untrue. The parens aren't strictly requird. The grammar of sizeof
is:

sizeof unary-expression // note no parens here.
swizeof ( type-id )
you'd evaluate the expression inside the outer parens
first.


Parens have no bearing on order of evaulation in C++.
Jul 19 '05 #8

"jeffc" <no****@nowhere .com> wrote in message
news:3f******** @news1.prserv.n et...

"Roy Yao" <fl***@263.ne t> wrote in message
news:bk******** ***@mail.cn99.c om...
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 ?
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.


Additionally, his second option: (sizeof(int)*(p )) is exactly the same as
the one in his subject line.
DrX.
Jul 19 '05 #9

"jeffc" <no****@nowhere .com> wrote in message
news:3f******** @news1.prserv.n et...

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.


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.
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1689
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (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
5
3604
by: aarklon | last post by:
Hi all, why printf("....%d",sizeof((int)(double)(char) i)) always gives the size of int ??? is it because sizeof doesn't evaluate its operand....???
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.