C++/Pointer/Pointer Primitive
Содержание
- 1 Assign the public object member address to a pointer
- 2 Declares a pointer and then outputs its value without first assigning
- 3 Decrementing a Pointer for int value
- 4 How to use the address operator to assign the address of a variable to a pointer
- 5 Indirection Operator and Dereferencing
- 6 Null Pointers
- 7 Pointer as a Variable
- 8 Pointers to Class Members
- 9 Prints the values and addresses of variables.
- 10 Returning a pointer
- 11 The actual data type of the value of all pointers is the same
- 12 Using a reference parameter for class type
Assign the public object member address to a pointer
<source lang="cpp">
- include <iostream>
using namespace std; class MyClass { public:
int i; MyClass(int j) { i = j; }
}; int main() {
MyClass myObject(1); int *p; p = &myObject.i; cout << *p; return 0;
}
</source>
Declares a pointer and then outputs its value without first assigning
<source lang="cpp">
- include <iostream>
using namespace std; int main () {
int* intPointer; cout << "The value of intPointer is " << intPointer << endl; return 0;
}
</source>
Decrementing a Pointer for int value
<source lang="cpp">
- include <iostream>
using namespace std; const int Length = 3; int main () {
int testScore[Length] = {4, 7, 1}; int* intPointer = &testScore[Length - 1]; int i = Length - 1; while (intPointer >= &testScore[0]) { cout << "The address of index " << i << " of the array is "<< intPointer << endl; cout << "The value at index " << i << " of the array is "<< *intPointer << endl; intPointer--; i--; } return 0;
}
</source>
How to use the address operator to assign the address of a variable to a pointer
<source lang="cpp">
- include <iostream>
using namespace std; int main () {
int num = 5; int* intPointer = # cout << "The address of x using &num is " << &num << endl; cout << "The address of x using intPointer is " << intPointer << endl; return 0;
}
</source>
Indirection Operator and Dereferencing
<source lang="cpp">
- include <iostream>
using namespace std; int main () {
int num = 5; int* intPointer = # cout << "The value of num is " << num << endl; num = 10; cout << "The value of num after num = 10 is " << num << endl; *intPointer = 15; cout << "The value of num after *intPointer = 15 is " << num << endl; return 0;
}
</source>
Null Pointers
<source lang="cpp">
- include <iostream>
using namespace std; int main () {
int* intPointer; intPointer = NULL; cout << "The value of intPointer is " << intPointer << endl; return 0;
}
</source>
Pointer as a Variable
<source lang="cpp">
- include <iostream>
using namespace std; int main () {
int num1 = 5, num2 = 14; int* intPointer = &num1; cout << "The value of num1 is " << num1 << endl; *intPointer *= 2; cout << "The value of num1 after *intPointer *= 2 is " << *intPointer << endl; intPointer = &num2; cout << "The value of num2 is " << num2 << endl; *intPointer /= 2; cout << "The value of num after *intPointer /= 2 is " << *intPointer << endl; return 0;
}
</source>
Pointers to Class Members
<source lang="cpp">
- include <iostream>
using namespace std; class MyClass { public:
MyClass(int i) { val=i; } int val; int doubleValue() { return val+val; }
}; int main() {
int MyClass::*data; int (MyClass::*func)(); MyClass myObject1(1), myObject2(2); data = &MyClass::val; func = &MyClass::doubleValue; cout << "Here are values: "; cout << myObject1.*data << " " << myObject2.*data << "\n"; cout << "Here they are doubled: "; cout << (myObject1.*func)() << " "; cout << (myObject2.*func)() << "\n"; return 0;
}
</source>
Prints the values and addresses of variables.
<source lang="cpp">
- include <iostream>
using namespace std; int var, *ptr; int main() {
var = 100; ptr = &var; cout << " Value of var: " << var << " Address of var: " << &var << endl; cout << " Value of ptr: " << ptr << " Address of ptr: " << &ptr << endl; return 0;
}
</source>
Returning a pointer
<source lang="cpp">
- include <iostream>
- include <string>
- include <vector>
using namespace std; //returns a pointer to a string element string* ptrToElement(vector<string>* const pVec, int i); int main() {
vector<string> v; v.push_back("A"); v.push_back("B"); v.push_back("C"); cout << *(ptrToElement(&v, 0)) << endl; string* pStr = ptrToElement(&v, 1); cout << *pStr << endl; string str = *(ptrToElement(&v, 2)); cout << str << endl; *pStr = "Healing Potion"; cout << v[1] << endl; return 0;
} string* ptrToElement(vector<string>* const pVec, int i) {
//returns address of the string in position i of vector that pVec points to return &((*pVec)[i]);
}
</source>
The actual data type of the value of all pointers is the same
<source lang="cpp">
- include <iostream>
using namespace std; int main () {
int* intPointer; float* floatPointer; char *charPointer; cout << "The size of intPointer is " << sizeof(intPointer) << endl; cout << "The size of floatPointer is " << sizeof(floatPointer) << endl; cout << "The size of charPointer is " << sizeof(charPointer) << endl; return 0;
}
</source>
Using a reference parameter for class type
<source lang="cpp">
- include <iostream>
- include <cstring>
- include <cstdlib>
using namespace std; class StringClass {
char *p;
public:
StringClass(char *s); ~StringClass() { delete [] p; } char *get() { return p; }
}; StringClass::StringClass(char *s) {
int l; l = strlen(s)+1; p = new char [l]; if(!p) { cout << "Allocation error\n"; exit(1); } strcpy(p, s);
} // Using a reference parameter. void show(StringClass &x) {
char *s; s = x.get(); cout << s << endl;
} int main() {
StringClass stringObject1("Hello"), stringObject2("There"); show(stringObject1); show(stringObject2); return 0;
}
</source>