What does this mean?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Ex4_07.cpp
// Initializing pointers with strings
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char* pstr[] = { "Robert Redford", // Initializing a pointer array
"Hopalong Cassidy",
"Lassie",
"Slim Pickens",
"Boris Karloff",
"Oliver Hardy"
};
char* pstart("Your lucky star is ");
int dice(0);
cout << endl
<< "Pick a lucky star!"
<< "Enter a number between 1 and 6: ";
cin >> dice;
cout << endl;
if(dice >= 1 && dice <= 6) // Check input validity
cout << pstart << pstr[dice - 1]; // Output star name
else
cout << "Sorry, you haven't got a lucky star."; // Invalid input
cout << endl;
return 0;
}



How do I swap the pointers as described in this paragraph?

Space saving isn’t the only advantage that you get by using pointers. In a lot of circumstances you save
time, too. Think of what happens if you want to move "Oliver Hardy" to the fi rst position and "Robert
Redford" to the end. With the pointer array as in Ex4_07.cpp, you just need to swap the pointers — the
strings themselves stay where they are. If you had stored these simply as strings as you did in Ex4_04.cpp,
a great deal of copying would be necessary — you’d need to copy the whole string "Robert Redford"
to a temporary location while you copied "Oliver Hardy" in its place, and then you’d need to copy
"Robert Redford" to the end position. This requires signifi cantly more computer time to execute.

Can someone help me please?
i have no any Idea....
How do I swap the pointers as described in this paragraph?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <algorithm>

int main()
{
    using std::cout;

    const char* pstr[] = // *** const required, added
    {
        "Robert Redford",
        "Hopalong Cassidy",
        "Lassie",
        "Slim Pickens",
        "Boris Karloff",
        "Oliver Hardy"
    };
    const int n = sizeof(pstr) / sizeof( pstr[0] ) ;
    cout << "at start:\n\tat position 0 we have " << pstr[0]
          << "\n\tand at the end we have " << pstr[n-1] << '\n' ;

    // move "Oliver Hardy" to the first position and
    // "Robert Redford" to the end.
    std::swap( pstr[0], pstr[n-1] ) ; // use std::swap() in <algorithm> (recommended)
    cout << "after swap:\n\tat position 0 we have " << pstr[0]
          << "\n\tand at the end we have " << pstr[n-1] << '\n' ;

    // move "Oliver Hardy" back to the end and
    // "Robert Redford" back the first position
    const char* temp = pstr[0] ; // swap using a temporarary variable
    pstr[0] = pstr[n-1] ;
    pstr[n-1] = temp ;
    cout << "after swap again:\n\tat position 0 we have " << pstr[0]
          << "\n\tand at the end we have " << pstr[n-1] << '\n' ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Ex4_07.cpp
// Initializing pointers with strings
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

void Swap(char **S1, char **S2)
{
  cout << "\tSwapped " << *S1 << " and " << *S2 << endl << endl;
  char *B(*S1);
  *S1 = *S2;
  *S2 = B;  
}

void Print(char** start, char **last)
{
  while(start != last)
    cout << *start++ << ", ";
  cout << endl << endl;
}
int main()
{
  char* pstr[] = {"Robert Redford","Hopalong Cassidy", "Lassie",
  "Slim Pickens",
  "Boris Karloff",
  "Oliver Hardy" };// Initializing a pointer array
  
  char* pstart("Your lucky star is ");
  //char *temp;
  int dice(0);
  cout << endl
  << "Pick a lucky star!"
  << "Enter a number between 1 and 6: ";
  cin >> dice;
  cout << endl;
  if(dice >= 1 && dice <= 6){ // Check input validity
    cout << pstart << pstr[dice - 1]; // Output star name
  }
  else{
    cout << "Sorry, you haven't got a lucky star."; // Invalid input
  }
  cout << endl << endl;
  
  
  Print(pstr, pstr+6);//Initial is unchanged
  Swap(pstr+1, pstr+3);//Swap 2nd and 4th persons
  Print(pstr, pstr+6);
  Swap(pstr, pstr+3);//Swapped 1st and 4th persons
  Print(pstr, pstr+6);
  Swap(pstr+2, pstr+5);//Swapped 3rd and last persons
  Print(pstr, pstr+6);
  return 0;
}
Last edited on
Topic archived. No new replies allowed.