Need help w/ Pointer / Arrays -> Accessing characters of strings

Hi, I am having trouble trying to figure out a way to use an array of pointers (which points to different strings) to be able to access each letter of the string instead of the whole string at once.


here is part of my main function:
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
int main()
{
   unsigned long max;
   string natural = ""; // bitstring to hold numbers 0, 1, 2, 3, 4, ...
   string evens = "";   // bitstring to hold numbers 2, 4, 6, 8, 10, ...
   string odds = "";    // bitstring to hold numbers 1, 3, 5, 7, 9, ...
   string fibonacci = ""; // bitstring to hold numbers 0, 1, 1, 2, 3, 5, ...
   string *sets[4];
   sets[0] = ♮
   sets[1] = &evens;
   sets[2] = &odds;
   sets[3] = &fibonacci;
   char option;

   // prompt for max / limit
   cout << "How big would you like to display each set?\n"
        << "(Please insert a value between 0 and 18,446,744,073,709,551,615)\n"
        << ">";
   cin >> max;
   //cerr << "you have selected: " << max << endl;

   // fill set arrays
   cout << "Generating Sets...\n";
   for (int i = 0; i < max; i++)
   {
      natural += "1";
      if ((i % 2) && (i > 0))
      {
         evens += "0";
         odds  += "1"; // the number is odd

      }
      else if (i > 0)
      {
         evens += "1"; // the number is even
         odds  += "0";
      }
      else // the first number is a 0, therefore neither even nor odd
      {
         evens += "0";
         odds  += "0";
      }
   }

   for (int i = 0; i < 4; i++)
     cerr << *sets[i] << endl; // this displays the string sets using the 
                               //      pointer
   cerr << "first element of even numbers: " << sets[1][0] << endl;

   cerr << "Set of Naturals: " << natural << endl;
   cerr << "Set of Evens: " << evens << endl;
   cerr << "Set of Odds: " << odds << endl;
...


Here is the program output
How big would you like to display each set?
(Please insert a value between 0 and 18,446,744,073,709,551,615)
>12
Generating Sets...
111111111111
001010101010
010101010101

first element of even numbers: 001010101010
Set of Naturals: 111111111111
Set of Evens: 001010101010
Set of Odds: 010101010101


How can I be able to use the pointer to access and compare each letter of the string? With the given example, I "displayed" sets[1][0], but the output displays the whole string, when I wanted only the first letter...

Does anybody know what can I do? I could pass each set string to my functions, but then I would have a lot of parameters, so that's why I decided to just pass an array of pointers, but I can't figure out a way to use the pointers to access each letter of the string...

PS:
nvm, I think that I've discovered a way to do it: I have to use
sets[1][0][0]
To display the first letter of the string evens.

However would someone care to explain the need to the [0] in the middle?
As far as I understood, the first [] points to a given string (either natural numbers, or even numbers, or ...), and the third [] points to the character, however I don't see the reason of [0] in the middle... =/
Last edited on
The reason for the [0] in the middle is that you have an array of pointers to strings, instead of an array of strings. Your sets[1] is resulting in a pointer. For plain pointers, p[0] is the same as *p, so sets[1][0] is the same as *sets[1], which is a reference to a string. Once you have a string, you can [0] to get the character

Make it

1
2
3
4
5
   string sets[4]; // no *
   sets[0] = natural; // no &
   sets[1] = evens;
   sets[2] = odds;
   sets[3] = fibonacci;

and you won't need to introduce this unnecessary dereference.
I see what you mean.
My intention at first was to have an array of pointers to strings because after I fill in the arrays, those pointers would point to the updated data, and also because those arrays could become really big and I didn't wanna waste space by creating a new array with all those huge strings...

but If I go about doing in the way you suggested, then I would have to move that code to after the part in which I fill in the arrays. (which is no big deal).
Thx for the reply =)
Last edited on
Topic archived. No new replies allowed.