C++ Tutorial/Data Types/int pointer — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
|
(нет различий)
|
Версия 17:21, 25 мая 2010
Calculation based on pointer
<source lang="cpp">#include <iostream> using namespace std;
int main() {
int *p, num; p = # *p = 100; cout << num << " "; (*p)++; cout << num << " "; (*p)--; cout << num << "\n"; return 0;
}</source>
100 101 100
Get address of an int variable
<source lang="cpp">#include <iostream> using namespace std;
int main() {
int total; int *ptr; int val; total = 3200; // assign 3,200 to total ptr = &total; // get address of total val = *ptr; // get value at that addres cout << "Total is: " << val << "\n"; return 0;
}</source>
Total is: 3200
Using int pointers
<source lang="cpp">#include <iostream>
using std::cout; // this file uses std::cout int main() { int intValue; // a variable int * intPointer = 0; // a pointer intValue = 5; intPointer = &intValue; // assign address of intValue to intPointer cout << "intValue: " << intValue << "\n"; cout << "*intPointer: " << *intPointer << "\n\n"; cout << "*intPointer = 7\n"; *intPointer = 7; // sets intValue to 7 cout << "*intPointer: " << *intPointer << "\n"; cout << "intValue: " << intValue << "\n\n"; cout << "intValue = 9\n"; intValue = 9; cout << "intValue: " << intValue << "\n"; cout << "*intPointer: " << *intPointer << "\n"; return 0; }</source>
intValue: 5 *intPointer: 5 *intPointer = 7 *intPointer: 7 intValue: 7 intValue = 9 intValue: 9 *intPointer: 9