Arrays of pointers...

This is a discussion on Arrays of pointers... within the C++ Programming forums, part of the General Programming Boards category; For the record, here is the code that works with an array of char pointers (now free of all bugs ...

  1. #61
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    For the record, here is the code that works with an array of char pointers (now free of all bugs -- I hope):
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct stringStruct {
        stringStruct() : arrayOfPointers(NULL) { initialize(); }
        void initialize();
        string* str;
        string* anotherStr;
        const char** arrayOfPointers;
    };
    
    void stringStruct::initialize() {
        str = NULL;
        anotherStr = NULL;
    }
    
    void doStuff();
    
    int main() {
      doStuff();
      cout<< "\nHaha, you got to press Enter."<<endl;
      cin.get();
      return 0;
    }
    
    void doStuff() {
      stringStruct object;
      object.str = new string;
      *object.str = "Yes, a string...\n";
      *object.str += "Yes, another string...\n";
      *object.str += "Yes, even another string...\n";
      int sizeOfStr = object.str->size();
      object.anotherStr = new string;
      int sizeOfArrayOfPointers = 3;
      object.arrayOfPointers = new const char*[sizeOfArrayOfPointers];
      int i2 = 0;
      for (int i = 0; i < sizeOfStr; i++) {
        if (object.anotherStr == NULL) {
            object.anotherStr = new string;
        }
    
        if (object.str->at(i2) == '\n') { //we're at a new-line character
            i2++; //increment so as to skip the new-line character to the next character, which wont be a new-line character
        }
    
        if (i == sizeOfArrayOfPointers - 1) { //we're at the last element of the array of pointers
            //continue looping through object.str...
            while (object.str->at(i2) != '\n') {
                *object.anotherStr += object.str->at(i2);
                i2++;
            }
            object.arrayOfPointers[i] = object.anotherStr->data();
            for (int i = 0; i < sizeOfArrayOfPointers; i++) {
                cout<< object.arrayOfPointers[i] <<endl;
            }
            
            if (object.arrayOfPointers) {
              delete [] object.arrayOfPointers;
            }
    
            if (object.anotherStr) {
              delete object.anotherStr;
              object.anotherStr = NULL;
            }
            
            return; //out of the function
        }
    
        while (object.str->at(i2) != '\n') { //iterate till the next new-line character
          *object.anotherStr += object.str->at(i2); //add characters to the "anotherStr"
          i2++;
        }
    
        object.arrayOfPointers[i] = object.anotherStr->data();
        object.anotherStr = NULL; //reset this pointer
      }
    
    }
    And here is the code with the vector that I ended up using in place of the array of pointers:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main() {
      string *str = new string;
      *str = "Yes, a string...\n";
      *str += "Yes, another string...\n";
      *str += "Yes, even another string...\n";
      string* anotherStr = new string;
      int sizeOfStr = str->size();
      vector <string> aVectorOfStrings; //create a vector for storing individual strings
      int i2 = 0;
      for (int i = 0; i < sizeOfStr; i++) {
        if (anotherStr == NULL) {
          anotherStr = new string;
        }
    
        if (str->at(i2) == '\n') {
          i2++; //skip past the new-line character
        }
    
        while (str->at(i2) != '\n') { //read up until the next new-line character
          *anotherStr += str->at(i2); //add characters to the string
          i2++;
        }
        aVectorOfStrings.push_back(*anotherStr); //add a new string to the vector of strings
        delete anotherStr;
        anotherStr = NULL; //reset the pointer
    
        if (i == 2) { //we just added the last string to the vector...
            break; //out of the loop
        }
      }
    
      int sizeOfVector = aVectorOfStrings.size();
      for (int i = 0; i < sizeOfVector; i++) {
        cout<< aVectorOfStrings.at(i) <<endl;
      }
    
      cout<< "\nHaha, you got to press Enter..."<<endl;
      cin.get(); //make the user press enter
      return 0;
    }
    Both work as expected, but the vector method is obviously preferred.
    Last edited by Programmer_P; 05-24-2010 at 09:53 PM.

Page 5 of 5 FirstFirst 12345
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with arrays, pointers, and structs.
    By RexInTheCity in forum C Programming
    Replies: 5
    Last Post: 03-29-2010, 03:30 PM
  2. Pointers As 2D arrays
    By TieFighter in forum C Programming
    Replies: 29
    Last Post: 03-22-2010, 06:46 AM
  3. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 06:28 AM
  4. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  5. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21