C++ Tutorial/Pointer/address of

Материал из C\C++ эксперт
Версия от 10:30, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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;
}