gogllens.blogg.se

Indirection operator
Indirection operator




  1. #INDIRECTION OPERATOR HOW TO#
  2. #INDIRECTION OPERATOR CODE#

The following example demonstrates the behavior of both postfix and prefix increment operators: The result of ++p and -p is the value of p after the operation. The result of p++ and p- is the value of p before the operation. The - decrement operator subtracts 1 from its pointer operand.īoth operators are supported in two forms: postfix ( p++ and p-) and prefix ( ++p and -p). The ++ increment operator adds 1 to its pointer operand. The following example demonstrates the pointer subtraction: That is, p1 - p2 is computed as ((long)(p1) - (long)(p2)) / sizeof(T). Pointer subtractionįor two pointers p1 and p2 of type T*, the expression p1 - p2 produces the difference between the addresses given by p1 and p2 divided by sizeof(T). The following example demonstrates the usage of the + operator with a pointer: The sizeof operator obtains the size of a type in bytes. The p - n expression produces a pointer of type T* that results from subtracting n * sizeof(T) from the address given by p.Both p + n and n + p expressions produce a pointer of type T* that results from adding n * sizeof(T) to the address given by p.Addition or subtraction of an integral value to or from a pointerįor a pointer p of type T* and an expression n of a type implicitly convertible to int, uint, long, or ulong, addition and subtraction are defined as follows: You cannot perform those operations with pointers of type void*.įor information about supported arithmetic operations with numeric types, see Arithmetic operators. Add or subtract an integral value to or from a pointer.You can perform the following arithmetic operations with pointers: You can also use the operator for array element or indexer access. You cannot use for pointer element access with an expression of type void*. The pointer element access operator doesn't check for out-of-bounds errors. In the preceding example, a stackalloc expression allocates a block of memory on the stack.

#INDIRECTION OPERATOR HOW TO#

The following example demonstrates how to access array elements with a pointer and the operator: For information about the behavior of the + operator with pointers, see the Addition or subtraction of an integral value to or from a pointer section. Pointer element access operator įor an expression p of a pointer type, a pointer element access of the form p is evaluated as *(p + n), where n must be of a type implicitly convertible to int, uint, long, or ulong. You cannot apply the -> operator to an expression of type void*. The following example demonstrates the usage of the -> operator: That is, if x is a pointer of type T* and y is an accessible member of type T, an expression of the form The -> operator combines pointer indirection and member access. The binary * operator computes the product of its numeric operands. You cannot apply the * operator to an expression of type void*. The operand of the * operator must be of a pointer type. It's also known as the dereference operator. The unary pointer indirection operator * obtains the variable to which its operand points. The binary & operator computes the logical AND of its Boolean operands or the bitwise logical AND of its integral operands. You can't get the address of a constant or a value.įor more information about fixed and movable variables, see the Fixed and moveable variables section of the C# language specification.

indirection operator

The following example shows how to use a fixed statement and the & operator: The obtained address is valid only inside the block of a fixed statement. You can get the address of a movable variable if you "fix", or "pin", it with a fixed statement. Object fields and array elements are examples of movable variables. Variables that reside in storage locations that can be affected by the garbage collector (for example, relocated) are called movable variables. In the preceding example, the local variable number is a fixed variable, because it resides on the stack.

indirection operator

Fixed variables are variables that reside in storage locations that are unaffected by operation of the garbage collector. The operand of the & operator must be a fixed variable. The unary & operator returns the address of its operand:

indirection operator

#INDIRECTION OPERATOR CODE#

The code that contains unsafe blocks must be compiled with the AllowUnsafeBlocks compiler option. The -> (member access) and (element access) operatorsįor information about pointer types, see Pointer types.Īny operation with pointers requires an unsafe context.Unary * (pointer indirection) operator: to obtain the variable pointed by a pointer.Unary & (address-of) operator: to get the address of a variable.You can use the following operators to work with pointers:






Indirection operator