[ Pobierz całość w formacie PDF ]
expression and the operand is decremented.
For example:
play = --play1 + play2--;
is similar to the following expressions;play2is altered beforeplay:
int temp, temp1, temp2;
temp1 = play1;
temp2 = play2 - 1;
play1 = play1 - 1;
temp = temp1 + temp2;
play2 = temp2;
play = temp;
The result has the same type as the operand after integral promotion, but is not an
lvalue.
The usual arithmetic conversions are performed on the operand.
v Pointer Arithmetic on page 60
v Lvalues and Rvalues on page 75
v Arithmetic Conversions on page 121
Unary Plus +
The+(unary plus) operator maintains the value of the operand. The operand can
have any arithmetic type or pointer type. The result is not an lvalue.
The result has the same type as the operand after integral promotion.
88 C/C++ Language Reference
Unary Expressions
Note: Any plus sign in front of a constant is not part of the constant.
v Lvalues and Rvalues on page 75
Unary Minus -
The-(unary minus) operator negates the value of the operand. The operand can
have any arithmetic type. The result is not an lvalue.
For example, ifqualityhas the value100,-qualityhas the value-100.
The result has the same type as the operand after integral promotion.
Note: Any minus sign in front of a constant is not part of the constant.
v Lvalues and Rvalues on page 75
Logical Negation !
The!(logical negation) operator determines whether the operand evaluates to0
(false) or nonzero (true).
The expression yields the value1(true) if the operand evaluates to0, and
yields the value0(false) if the operand evaluates to a nonzero value.
The expression yields the value true if the operatand evaluate to false (0),
and yields the value false if the operand evaluates to true (nonzero). The operand
is implicitly converted to bool and the type of the result is bool.
The following two expressions are equivalent:
!right;
right == 0;
v Lvalues and Rvalues on page 75
v Boolean Variables on page 33
Bitwise Negation Ü
TheÜ(bitwise negation) operator yields the bitwise complement of the operand. In
the binary representation of the result, every bit has the opposite value of the same
bit in the binary representation of the operand. The operand must have an integral
type. The result has the same type as the operand but is not an lvalue.
Supposexrepresents the decimal value5. The 16-bit binary representation ofxis:
0000000000000101
The expressionÜxyields the following result (represented here as a 16-bit binary
number):
1111111111111010
Note that theÜcharacter can be represented by the trigraph??-.
The 16-bit binary representation ofÜ0is:
Chapter 5. Expressions and Operators 89
Unary Expressions
1111111111111111
v Lvalues and Rvalues on page 75
v Trigraph Sequences on page 12
Address &
The&(address) operator yields a pointer to its operand. The operand must be an
lvalue, a function designator, or a qualified name. It cannot be a bit field, nor can it
have the storage class register.
You may take the address of a register variable.
If the operand is an lvalue or function, the resulting type is a pointer to the
expression type. For example, if the expression has type int, the result is a pointer
to an object having type int.
If the operand is a qualified name and the member is not static, the result is a
pointer to a member of class and has the same type as the member. The result is
not an lvalue.
Ifp_to_yis defined as a pointer to an int andyas an int, the following expression
assigns the address of the variableyto the pointerp_to_y:
p_to_y = &y;
You can use the&operator with overloaded functions only in an
initialization or assignment where the left side uniquely determines which version
of the overloaded function is used.
v Lvalues and Rvalues on page 75
v Chapter 7. Functions on page 123
v Pointers on page 58
v Overloading Functions on page 185
v register Storage Class Specifier on page 27
Indirection *
The*(indirection) operator determines the value referred to by the pointer-type
operand. The operand cannot be a pointer to an incomplete type. The operation
yields an lvalue or a function designator if the operand points to a function.
Arrays and functions are converted to pointers.
The type of the operand determines the type of the result. For example, if the
operand is a pointer to an int, the result has type int.
Do not apply the indirection operator to any pointer that contains an address that
is not valid, such asNULL. The result is not defined.
Ifp_to_yis defined as a pointer to an int andyas an int, the expressions:
p_to_y = &y;
*p_to_y = 3;
cause the variableyto receive the value3.
90 C/C++ Language Reference
Unary Expressions
v Arrays on page 62
v Chapter 7. Functions on page 123
v Pointers on page 58
sizeof (Size of an Object)
The sizeof operator yields the size in bytes of the operand. Types cannot be defined
in a sizeof expression. The sizeof operation cannot be performed on
v A bit field
v A function
v An undefined structure or class
v An incomplete type (such as void)
The operand can be the parenthesized name of a type or expression.
The compiler must be able to evaluate the size at compile time. The expression is
not evaluated; there are no side effects. For example, the value ofbis 5 from
initialization to the end of program runtime:
#include
int main(void){
int b = 5;
sizeof(b++);
return 0;
}
The result is an integer constant.
The size of a char object is the size of a byte. For example, if a variablexhas type
char, the expressionsizeof(x)always evaluates to1.
The result of a sizeof operation has type size_t, which is an unsigned integral type
defined in theheader.
The size of an object is determined on the basis of its definition. The sizeof
operator does not perform any conversions. If the operand contains operators that
perform conversions, the compiler does take these conversions into consideration.
The following expression causes the usual arithmetic conversions to be performed.
The result of the expressionx + 1has type int (ifxhas typechar, short, or int or
any enumeration type) and is equivalent tosizeof(int):
sizeof (x + 1);
Except in preprocessor directives, you can use a sizeof expression wherever an
integral constant is required. One of the most common uses for the sizeof operator
is to determine the size of objects that are referred to during storage allocation,
input, and output functions.
Another use of sizeof is in porting code across platforms. You should use the
sizeof operator to determine the size that a data type represents. For example:
sizeof(int);
The result of a sizeof expression depends on the type it is applied to:
An array The result is the total number of bytes in the array. For example, in
an array with 10 elements, the size is equal to 10 times the size of
Chapter 5. Expressions and Operators 91
Unary Expressions
a single element. The compiler does not convert the array to a
pointer before evaluating the expression.
A class The result is always nonzero, and is equal to the number of bytes
in an object of that class including any padding required for
placing class objects in an array.
A reference The result is the size of the referenced object.
v Integer Constant Expressions on page 75
v Arrays on page 62
v Chapter 12. Classes on page 199
v References on page 68
C++ new Operator
The new operator provides dynamic storage allocation. The syntax for an
allocation expression containing the new operator is:
new ( type )
[ Pobierz całość w formacie PDF ]