C++ Tutorial/Array/array pointer
Содержание
- 1 an array of pointers to strings
- 2 array accessed with array notation
- 3 array accessed with pointer
- 4 array accessed with pointer notation
- 5 Array pointer
- 6 Index a pointer as if it were an array
- 7 Output array address by using array pointer
- 8 passing array as a constant pointer
- 9 Relationship between pointers and arrays
- 10 Use a 2-D array of pointers to create a dictionary
an array of pointers to strings
<source lang="cpp">#include <iostream>
using namespace std; const int DAYS = 7; int main(){ char* arrptrs[DAYS] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; for(int j=0; j<DAYS; j++) cout << arrptrs[j] << endl; return 0; }</source>
array accessed with array notation
<source lang="cpp">#include <iostream>
using namespace std; int main(){ int intarray[5] = { 31, 54, 77, 52, 93 }; for(int j=0; j<5; j++) cout << intarray[j] << endl; return 0; }</source>
array accessed with pointer
<source lang="cpp">#include <iostream>
using namespace std; int main(){ int intarray[] = { 31, 54, 77, 52, 93 }; int* ptrint; ptrint = intarray; for(int j=0; j<5; j++) cout << *(ptrint++) << endl; return 0; }</source>
array accessed with pointer notation
<source lang="cpp">#include <iostream>
using namespace std; int main(){ int intarray[5] = { 31, 54, 77, 52, 93 }; for(int j=0; j<5; j++) cout << *(intarray+j) << endl; return 0; }</source>
Array pointer
<source lang="cpp">#include <iostream> using namespace std;
int main() {
int *i, j[10]; double *f, g[10]; int x; i = j; f = g; for(x=0; x<10; x++) cout << i+x << " " << f+x << "\n"; return 0;
}</source>
0x22ff30 0x22fed0 0x22ff34 0x22fed8 0x22ff38 0x22fee0 0x22ff3c 0x22fee8 0x22ff40 0x22fef0 0x22ff44 0x22fef8 0x22ff48 0x22ff00 0x22ff4c 0x22ff08 0x22ff50 0x22ff10 0x22ff54 0x22ff18
Index a pointer as if it were an array
<source lang="cpp">#include <iostream>
- include <cctype>
using namespace std;
int main() {
char *p; int i; char str[80] = "This Is A Test"; cout << "Original string: " << str << "\n"; p = str; for(i = 0; p[i]; i++) { if(isupper(p[i])) p[i] = tolower(p[i]); else if(islower(p[i])) p[i] = toupper(p[i]); } cout << "Inverted-case string: " << str; return 0;
}</source>
Original string: This Is A Test Inverted-case string: tHIS iS a tEST
Output array address by using array pointer
<source lang="cpp">#include <assert.h>
- include <iostream>
- include <iomanip>
char array[10] = "012345678"; int main() {
for (int i = 0; i < 10; ++i) { std::cout << std::hex; std::cout << "&array[i]=0x" << reinterpret_cast<int>(&array[i]) << " (array+i)=0x" << reinterpret_cast<int>(array+i) << " array[i]=0x" << static_cast<int>(array[i]) << "\n", std::cout << std::dec; } return (0);
}</source>
&array[i]=0x43d000 (array+i)=0x43d000 array[i]=0x30 &array[i]=0x43d001 (array+i)=0x43d001 array[i]=0x31 &array[i]=0x43d002 (array+i)=0x43d002 array[i]=0x32 &array[i]=0x43d003 (array+i)=0x43d003 array[i]=0x33 &array[i]=0x43d004 (array+i)=0x43d004 array[i]=0x34 &array[i]=0x43d005 (array+i)=0x43d005 array[i]=0x35 &array[i]=0x43d006 (array+i)=0x43d006 array[i]=0x36 &array[i]=0x43d007 (array+i)=0x43d007 array[i]=0x37 &array[i]=0x43d008 (array+i)=0x43d008 array[i]=0x38 &array[i]=0x43d009 (array+i)=0x43d009 array[i]=0x0
passing array as a constant pointer
<source lang="cpp">#include <iostream> using namespace std; void increase(int* const array, const int NUM_ELEMENTS); void display(const int* const array, const int NUM_ELEMENTS); int main() {
const int NUM_SCORES = 3; int highScores[NUM_SCORES] = {5, 3, 2}; cout << "passing array as a constant pointer.\n\n"; increase(highScores, NUM_SCORES); cout << "passing array as a constant pointer to a constant.\n"; display(highScores, NUM_SCORES); return 0;
} void increase(int* const array, const int NUM_ELEMENTS) {
for (int i = 0; i < NUM_ELEMENTS; ++i) array[i] += 500;
} void display(const int* const array, const int NUM_ELEMENTS) {
for (int i = 0; i < NUM_ELEMENTS; ++i) cout << array[i] << endl;
}</source>
Relationship between pointers and arrays
<source lang="cpp">#include <iostream> using namespace std; void increase(int* const array, const int NUM_ELEMENTS); void display(const int* const array, const int NUM_ELEMENTS); int main() {
const int NUM_SCORES = 3; int highScores[NUM_SCORES] = {5, 3, 2}; cout << "using array name as a constant pointer.\n"; cout << *highScores << endl; cout << *(highScores + 1) << endl; cout << *(highScores + 2) << "\n\n"; cout << "passing array as a constant pointer.\n\n"; increase(highScores, NUM_SCORES); cout << "passing array as a constant pointer to a constant.\n"; display(highScores, NUM_SCORES); return 0;
} void increase(int* const array, const int NUM_ELEMENTS) {
for (int i = 0; i < NUM_ELEMENTS; ++i) array[i] += 500;
} void display(const int* const array, const int NUM_ELEMENTS) {
for (int i = 0; i < NUM_ELEMENTS; ++i) cout << array[i] << endl;
}</source>
Use a 2-D array of pointers to create a dictionary
<source lang="cpp">#include <iostream>
- include <cstring>
using namespace std;
int main() {
char *dictionary[][2] = { "A", "AA", "B", "BB", "C", "CC", "D", "DD", "E", "EE", "", "" }; char word[80]; int i; cout << "Enter word: "; cin >> word; for(i = 0; *dictionary[i][0]; i++) { if(!strcmp(dictionary[i][0], word)) { cout << dictionary[i][1] << "\n"; break; } } if(!*dictionary[i][0]) cout << word << " not found.\n"; return 0;
}</source>
Enter word: word word not found.