Implicit pointer conversions 


Мы поможем в написании ваших работ!



ЗНАЕТЕ ЛИ ВЫ?

Implicit pointer conversions



From To
Any pointer type void*
null Any pointer type

Explicit pointer conversion is used to perform conversions, for which there is no implicit conversion, by using a cast expression. The following table shows these conversions.

Explicit pointer conversions

From To
Any pointer type Any other pointer type
sbyte, byte, short, ushort, int, uint, long, or ulong Any pointer type
Any pointer type sbyte, byte, short, ushort, int, uint, long, or ulong

 


Преобразования указателей

В следующей таблице показаны предопределенные неявные преобразования указателей. Неявные преобразования могут выполняться во многих ситуациях, включая вызов методов и операторы назначения.

Неявные преобразования указателей

Из В
Любой тип указателя void*
null Любой тип указателя

Явное преобразование указателя используется для выполнения преобразований, для которых невозможно неявное преобразование, с помощью выражения приведения. В следующей таблице приведено описание этих преобразований.

Явные преобразования указателей

Из В
Любой тип указателя Любой другой тип указателя
sbyte, byte, short, ushort, int, uint, long или ulong Любой тип указателя
Любой тип указателя sbyte, byte, short, ushort, int, uint, long или ulong

 


Example

In the following example, a pointer to int is converted to a pointer to byte. Notice that the pointer points to the lowest addressed byte of the variable. When you successively increment the result, up to the size of int (4 bytes), you can display the remaining bytes of the variable.

// compile with: /unsafe
class ClassConvert { static void Main() { int number = 1024;   unsafe { // Convert to byte: byte* p = (byte*)&number;   System.Console.Write("The 4 bytes of the integer:");   // Display the 4 bytes of the int variable: for (int i = 0; i < sizeof(int); ++i) { System.Console.Write(" {0:X2}", *p); // Increment the pointer: p++; } System.Console.WriteLine(); System.Console.WriteLine("The value of the integer: {0}", number); } } }

Output

The 4 bytes of the integer: 00 04 00 00

The value of the integer: 1024

 


Пример

В следующем пример указатель на int преобразуется в указатель на byte. Обратите внимание на то, что указатель указывает на наименьший адресуемый байт переменной. При последовательном увеличении результата до размера int (4 байта) можно отобразить оставшиеся байты переменной.

ß-----

 

 

Результат

The 4 bytes of the integer: 00 04 00 00

The value of the integer: 1024

 


Pointer Expressions

How to: Obtain the Value of a Pointer Variable

Use the pointer indirection operator to obtain the variable at the location pointed to by a pointer. The expression takes the following form, where p is a pointer type:

*p;

You cannot use the unary indirection operator on an expression of any type other than the pointer type. Also, you cannot apply it to a void pointer.

When you apply the indirection operator to a null pointer, the result depends on the implementation.

Example

In the following example, a variable of the type char is accessed by using pointers of different types. Note that the address of theChar will vary from run to run, because the physical address allocated to a variable can change.

// compile with: /unsafe
unsafe class TestClass { static void Main() { char theChar = 'Z'; char* pChar = &theChar; void* pVoid = pChar; int* pInt = (int*)pVoid;   System.Console.WriteLine("Value of theChar = {0}", theChar); System.Console.WriteLine("Address of theChar = {0:X2}",(int)pChar); System.Console.WriteLine("Value of pChar = {0}", *pChar); System.Console.WriteLine("Value of pInt = {0}", *pInt); } }

 

Value of theChar = Z Address of theChar = 12F718 Value of pChar = Z Value of pInt = 90

 


Выражения указателей



Поделиться:


Последнее изменение этой страницы: 2017-01-19; просмотров: 114; Нарушение авторского права страницы; Мы поможем в написании вашей работы!

infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 3.145.119.199 (0.005 с.)