C++/Pointer/Pointer Array

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Comparing Pointer Addresses

 

#include <iostream>
using namespace std;
const int Lengh = 3;
int main ()
{
   int testScore[Lengh] = {4, 7, 1};
   int* intPointer = testScore;
   int i = 0;
   while (intPointer <= &testScore[Lengh - 1])
   {
      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;
}


Incrementing a Pointer

 
#include <iostream>
using namespace std;
const int Lengh = 3;
int main ()
{
   int testScore[Lengh] = {4, 7, 1};
   int* intPointer = testScore;
   for (int i = 0; i < Lengh; i++, intPointer++)
   {
      cout << "The address of index " << i << " of the array is "<< intPointer << endl;
      cout << "The value at index " << i << " of the array is "<< *intPointer << endl;
   }
   return 0;
}


Index a pointer as if it were an array.

 
#include <iostream> 
#include <cctype> 
using namespace std; 
 
int main() 
{ 
  char *p; 
  int i; 
  char str[80] = "This Is A Test"; 
 
  cout << "Original string: " << str << endl; 
 
  p = str;                           // assign p the address of the start of the array 
 
  
  for(i = 0; p[i]; i++) {            // index p 
    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; 
}


The Array Name as a Constant Pointer

 
#include <iostream>
using namespace std;
const int Lengh = 3;
int main ()
{
   int testScore[Lengh] = {4, 7, 1};
   cout << "The address of the array using testScore is " << testScore << endl;
   cout << "The address of the first element of the array using &testScore[0] is " << &testScore[0] << endl;
   
   cout << "The value of the first element of the array using *testScore is " << *testScore << endl; 
   
   cout << "The value of the first element of the array using testScore[0] is " << testScore[0] << endl;
   return 0;
}


The name of the array is a constant pointer

 
#include <iostream>
using namespace std;
const int Lengh = 3;
int main ()
{
   int testScore[Lengh] = {4, 7, 1};
   int* intPointer = testScore;
   for (int i = 0; i < Lengh; i++)
   {
      cout << "The address of index " << i << " of the array is "<< & intPointer[i] << endl;
      cout << "The value at index " << i << " of the array is "<< intPointer[i] << endl;
   }
   return 0;
}


using array name as a constant pointer

  
#include <iostream>
using namespace std;
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";
    return 0;
}


Using a Variable Pointer to Point to an Array

 

#include <iostream>
using namespace std;
const int Lengh = 3;
int main ()
{
   int testScore[Lengh] = {4, 7, 1};
   for (int i = 0; i < Lengh; i++)
   {
      cout << "The address of index " << i << " of the array is "<< &testScore[i] << endl;
      cout << "The value at index " << i << " of the array is "<< testScore[i] << endl;
   }
   return 0;
}