C++ Tutorial/Pointer/address of — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:30, 25 мая 2010

Address of operator and addresses of local variables

#include <iostream>
 
 int main()
 {
     unsigned short shortVar=5;
     unsigned long  longVar= 5;
     long sVar = -5;
 
     std::cout << "shortVar:\t" << shortVar;
     std::cout << "\tAddress of shortVar:\t" << &shortVar << "\n";
     std::cout << "longVar:\t"  << longVar;
     std::cout << "\tAddress of longVar:\t"  << &longVar  << "\n";
     std::cout << "sVar:\t\t"   << sVar;
     std::cout << "\tAddress of sVar:\t"     << &sVar     << "\n";
 
     return 0;
 }
shortVar:       5       Address of shortVar:    0x22ff76
longVar:        5       Address of longVar:     0x22ff70
sVar:           -5      Address of sVar:        0x22ff6c

Demonstrating the Address-of Operator

#include <iostream>
int main()
{
   using namespace std;
   unsigned short shortVar=5;
   unsigned long  longVar=65535;
   long sVar = -65535;
   cout << shortVar;
   cout << "Address of shortVar:" << endl;
   cout <<  &shortVar  << endl;
   cout << longVar;
   cout << "Address of longVar:" << endl;
   cout <<  &longVar  << endl;
   cout << sVar;
   cout << "Address of sVar:" << endl;
   cout <<  &sVar << endl;
   return 0;
}

Printing the address stored in a char* variable

#include <iostream>
using namespace std;
int main()
{
   char *string = "test";
   cout << "Value of string is: " << string
        << "\nValue of static_cast< void *>( string ) is: " 
        << static_cast< void *>( string ) << endl;
   return 0;
}