Mikroelektronika MIKROE-724 データシート

ページ / 726
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
223
Sizeof Operator
The prefix unary operator 
sizeof
 returns an integer constant that represents the size of memory space (in bytes) used 
by its operand (determined by its type, with some exceptions).
The operator 
sizeof
 can take either a type identifier or an unary expression as an operand. You cannot use 
sizeof
 
with expressions of function type, incomplete types, parenthesized names of such types, or with lvalue that designates 
a bit field object.
Sizeof Applied to Expression
If applied to expression, the size of an operand is determined without evaluating the expression (and therefore without 
side effects). The result of the operation will be the size of the type of the expression’s result.
Sizeof Applied to Type
If applied to a type identifier, 
sizeof
 returns the size of the specified type. The unit for type size is 
sizeof(byte)
 
which is equivalent to one byte.
Thus:
sizeof(byte)            ‘ returns 1 
sizeof(integer)         ‘ returns 2 
sizeof(longword)        ‘ returns 4 
sizeof(float)            ‘ returns 4 
When the operand is a non-parameter of array type, the result is the total number of bytes in the array (in other words, 
an array name is not converted to a pointer type):
dim  i, j as integer
     samples as integer[7] 
...
j = sizeof(samples[1])  ‘ j = sizeof(integer) = 2 
i = sizeof(samples)     ‘ i = 10*sizeof(integer) = 20 
If the operand is a parameter declared as array type or function type, 
sizeof
 gives the size of the pointer. When 
applied to structures, 
sizeof
 gives the total number of bytes, including any padding. The operator 
sizeof
 cannot be 
applied to a function.