Thread: Arrays of pointers...

  1. #16
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Yep, I guessed that as soon as I realised that I did that.
    Anyway, the code was only an example, designed to sort of mirror what I did in my real code, only in the real code I initialized all variables or pointers to variables inside functions.

    Ok, here is what finally compiled:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct stringStruct {
        stringStruct();
        void initialize();
        string* str;
        string* anotherStr;
        const char** arrayOfPointers;
    };
    
    stringStruct::stringStruct() {
    
        initialize();
    
    }
    
    void stringStruct::initialize() {
        str = NULL;
        anotherStr = NULL;
    }
    
    void doStuff();
    
    int main() {
      doStuff();
      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 = 0;
      for (int i = 0; i < sizeOfStr; i++) {
          char currentChar = object.str->at(i);
          if (currentChar != '\n') {
              sizeOfArrayOfPointers++; //increment this
          }
      }
      object.arrayOfPointers = new const char*[sizeOfArrayOfPointers];
      for (int i = 0; i < sizeOfStr; i++) {
        while (object.str->at(i) != '\n') {
          object.anotherStr += object.str->at(i);
        }
        object.arrayOfPointers[i] = object.str->data();
        object.anotherStr = NULL; //reset this pointer
      }
      int sizeOfArray = sizeof(object.arrayOfPointers);
      for (int i = 0; i < sizeOfArray; i++) {
        cout<< *object.arrayOfPointers[i] <<endl;
      }
    
      delete object.str;
      delete [] object.arrayOfPointers;
      delete object.anotherStr;
    }
    However, it doesn't output the strings.
    Last edited by Programmer_P; 05-21-2010 at 03:49 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #17
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Wink

    Quote Originally Posted by rags_to_riches View Post
    Dude! The clumsiest "programming ninja" ever!
    Yep, well guess I should change it to "Programming Ninja In-Training" then...

    EDIT:
    Or make that "Programming Ninja In-T...". Ran out of allowed number of characters...
    Last edited by Programmer_P; 05-21-2010 at 02:51 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    *object.arrayofPointers[i] is just a single character. If you want the C-string, ditch the star.

  4. #19
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Well, I already tried that, but still nothing is outputted. It just gets stuck at a blank cursor in the terminal window.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I wish you the best of luck at getting out of this loop:
    Code:
     while (object.str->at(i) != '\n') {
          object.anotherStr += object.str->at(i);
        }

  6. #21
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Having a member that is a pointer to a string is virtually always wrong.
    Code:
    struct stringStruct {
        stringStruct() : arrayOfPointers(NULL) {}
        string str;
        string anotherStr;
        const char** arrayOfPointers;
    };
    FTFY.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #22
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    I wish you the best of luck at getting out of this loop:
    Code:
     while (object.str->at(i) != '\n') {
          object.anotherStr += object.str->at(i);
        }
    Oh...right.
    I can't believe I missed that. I obviously need a different variable than 'i'.

  8. #23
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Hmm...fixed that error, but now its printing out memory addresses.
    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();
      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 = 0;
      for (int i = 0; i < sizeOfStr; i++) {
          char currentChar = object.str->at(i);
          if (currentChar != '\n') {
              sizeOfArrayOfPointers++; //increment this
          }
      }
      object.arrayOfPointers = new const char*[sizeOfArrayOfPointers];
      for (int i = 0; i < sizeOfStr; i++) {
        int i2 = 0;
        while (object.str->at(i2) != '\n') {
          object.anotherStr += object.str->at(i2);
          i2++;
        }
        object.arrayOfPointers[i] = object.anotherStr->data();
        object.anotherStr = NULL; //reset this pointer
      }
      int sizeOfArray = sizeof(object.arrayOfPointers);
      for (int i = 0; i < sizeOfArray; i++) {
        cout<< object.arrayOfPointers[i] <<endl;
      }
    
      delete object.str;
      delete [] object.arrayOfPointers;
      delete object.anotherStr;
    }
    Last edited by Programmer_P; 05-21-2010 at 09:58 PM.

  9. #24
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Hmm you didn't listen. Let me restate it then:

    Having a member that is a pointer here is actually the wrong thing to do.

    The only thing it achieves is making this harder for yourself, and making the program longer.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #25
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by iMalc View Post
    Hmm you didn't listen. Let me restate it then:

    Having a member that is a pointer here is actually the wrong thing to do.

    The only thing it achieves is making this harder for yourself, and making the program longer.
    No, actually I just ignored that part, and added an initializer list to initialize the array of pointers to NULL like you have in your posted code a few posts back... So I still followed your advice, just not the statement about no member pointers.

    Do you think that it would output the strings, and not the memory addresses, if I did that though? If not, I wont bother changing it since its just a test program anyway.

  11. #26
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    And, btw, the reason I did that was because that's how I did it in my real program (only I have the array of pointers as a member of a class, instead of a struct). And the reason I did it that way is because its an array of pointers that I'm using across multiple member functions of the class. I prefer it that way, precisely because it makes it easier (NOT harder)...
    And so I create the array of pointers on the heap in one member function of the class, assign all the elements addresses of strings, then return a pointer to that array in another function. It makes the most sense, in my opinion.

    The problem is that even though I followed all advice given in this thread with the exception of the "no member pointers to strings" rule, it still outputs the addresses contained in the elements of the pointer array, instead of the strings contained at those addresses.
    Code:
    cout<< object.arrayOfPointers.[i] <<endl;
    does not about output the strings being pointed at by those pointer array elements like tabstop suggested it would, but instead outputs the memory addresses instead. I'm thinking that maybe if I try outputting the whole array, instead of individual elements, it'll output the whole strings. Only problem with that is, all strings will be on the same line...
    Last edited by Programmer_P; 05-21-2010 at 09:36 PM.

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    arrayofPointers was char**, n'est ce pas? I don't feel like going back up to find your code.

    The following does what I expect:
    Code:
    #include <iostream>
    #include <cstring>
    
    int main() {
        char **arrayofPointers;
        arrayofPointers = new char*[10];
        for (int i = 0; i < 10; ++i) {
            arrayofPointers[i] = new char[15];
            strcpy(arrayofPointers[i], "Hello world!");
        }
        for (int i = 0; i < 10; ++i) {
            std::cout << arrayofPointers[i] << std::endl;
        }
        return 0;
    }
    I'll leave it to you to compare and contrast. (We'll ignore the lack of delete[]s for the moment.)

  13. #28
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    arrayofPointers was char**, n'est ce pas? I don't feel like going back up to find your code.
    Yes, it is.
    The following does what I expect:
    Code:
    #include <iostream>
    #include <cstring>
    
    int main() {
        char **arrayofPointers;
        arrayofPointers = new char*[10];
        for (int i = 0; i < 10; ++i) {
            arrayofPointers[i] = new char[15];
            strcpy(arrayofPointers[i], "Hello world!");
        }
        for (int i = 0; i < 10; ++i) {
            std::cout << arrayofPointers[i] << std::endl;
        }
        return 0;
    }
    I'll leave it to you to compare and contrast. (We'll ignore the lack of delete[]s for the moment.)
    Do I really have to point every individual element of the array of pointers to a "new" allocation in dynamic memory? I thought the "arrayOfPointers = new char*[10]" part takes care of all elements. And anyway, seeing as I'm assigning (i.e. pointing) each element of the pointer of arrays to an already existing string, I would think I wouldn't have to do that.
    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();
      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 = 0;
      for (int i = 0; i < sizeOfStr; i++) {
          char currentChar = object.str->at(i);
          if (currentChar != '\n') {
              sizeOfArrayOfPointers++; //increment this
          }
      }
      object.arrayOfPointers = new const char*[sizeOfArrayOfPointers];
      int i2 = 0;
      for (int i = 0; i < sizeOfStr; i++) {
        while (object.str->at(i2) != '\n') {
          object.anotherStr += object.str->at(i2);
          i2++;
        }
        object.arrayOfPointers[i] = object.anotherStr->data();
        object.anotherStr = NULL; //reset this pointer
      }
      int sizeOfArray = sizeof(object.arrayOfPointers);
      for (int i = 0; i < sizeOfArray; i++) {
        cout<< object.arrayOfPointers[i] <<endl;
      }
    
      delete object.str;
      delete [] object.arrayOfPointers;
      delete object.anotherStr;
    }
    Last edited by Programmer_P; 05-21-2010 at 10:58 PM.

  14. #29
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Currently, when I run the code I just posted, I receive the following message:
    Segmentation fault

    Press ENTER to continue.
    I press Enter, and the program terminates. What its SUPPOSED to do is output the following:

    Yes, a string...
    Yes, another string...
    Yes, even another string...

    Press ENTER to continue.

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That code doesn't print any memory addresses. It just segfaults, when you try to access NULL the second time through that for loop around line 65.

    (EDIT: You're not actually accessing NULL -- you started the pointer out at NULL and you've been adding characters to the value of the pointer, so by the time you're done you're at some number in thousands, but that isn't actually a pointer to a string now is it.)
    Last edited by tabstop; 05-21-2010 at 10:23 PM.

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, 07: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, 04:49 PM