Thread: Arrays of pointers...

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

    Arrays of pointers...

    Hello,
    I have a problem. I need to make an array of pointers, without knowing beforehand the size of the array. Is this possible?

    Code:
    const char* arrayOfPointers[];

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Programmer_P View Post
    Hello,
    I have a problem. I need to make an array of pointers, without knowing beforehand the size of the array. Is this possible?

    Code:
    const char* arrayOfPointers[];
    Sure:

    Code:
    int main( void )
    {
        const char** arrayOfPointers = 0;
        // sometime later
        unsigned size = 1024;
        arrayOfPointers = new const char*[ size ];
        // do something usefull
        delete [ ] arrayOfPointers;
    }
    Thing is, though, you have to remember to delete the memory. A much better way is to use an std::vector, or similar, eg:

    Code:
    #include <vector>
    
    int main( void )
    {
        std::vector< const char* > arrayOfPointers;
        // sometime later
        unsigned size = 1024;
        arrayOfPointers.resize( size );
        // do something useful...memory will be deleted by vector object...
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Thank you.
    Quick question:

    Can an element of an array of pointers be assigned a string, or just a single character?

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    if it is an array of char * ptrs sure...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    How about this?
    Code:
    string* str = NULL;
    str = new string;
    *str = "Yes, a string...\n";
    *str += "Yes, another string...\n";
    *str += "Yes, even another string...\n";
    int sizeOfStr = str->size();
    char* arrayOfPointers[] = NULL;
    //several code lines later...
    arrayOfPointers = new char*[sizeOfStr];
    string* anotherStr = NULL;
    anotherStr = new string;
    
    void doStuff();
    void doStuff() {
      for (int = 0; i < sizeOfStr; i++) {
        while (str->at[i] != '\n') {
          *anotherStr += str->at[i];
        }
        *arrayOfPointers[i] = *anotherStr->data();
        anotherStr = NULL; //reset this pointer
      }
    }
    
    int main() {
      doStuff();
      int sizeOfArray = sizeof(arrayOfPointers);
      for (int = 0; i < sizeOfArray; i++) {
        cout<< *arrayOfPointers[i] <<endl;
      }
      
      delete str;
      delete [] arrayOfPointers;
      delete anotherStr;
      return 0;
    
    }
    Would the output be:
    Yes, a string...
    Yes, another string...
    Yes, even another string...
    ?
    Last edited by Programmer_P; 05-20-2010 at 07:53 PM.

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Bump.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Programmer_P View Post
    Would the output be:
    Why are you asking us? Don't you have a computer of your own? Why not try it and see?

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    Why are you asking us? Don't you have a computer of your own? Why not try it and see?
    For others' benefit.
    For the record, I tried it (though obviously including <string> and <iostream>), but it didn't compile. It said it expected constructor, destructor, or type conversion before the '=' token for the "string* str = NULL" line, as well as several lines after that. After changing the string pointer to a string object, it now says 'string' does not name a type, so obviously its not seeing the string class's definition for some reason, and I don't know why, seeing as I included it....
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Programmer_P View Post
    For others' benefit.
    For the record, I tried it (though obviously including <string> and <iostream>), but it didn't compile. It said it expected constructor, destructor, or type conversion before the '=' token for the "string* str = NULL" line, as well as several lines after that. After changing the string pointer to a string object, it now says 'string' does not name a type, so obviously its not seeing the string class's definition for some reason, and I don't know why, seeing as I included it....
    The <string> header doesn't give you a type called string. It does, however, give you a type called std::string.

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    The <string> header doesn't give you a type called string. It does, however, give you a type called std::string.
    Oh right..duh. I should have guessed that.
    Thanks.

    However, even after adding the line:

    Code:
    using namespace std;
    I still get the same errors about expecting constructor before '=' token.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Programmer_P View Post
    Oh right..duh. I should have guessed that.
    Thanks.

    However, even after adding the line:

    Code:
    using namespace std;
    I still get the same errors about expecting constructor before '=' token.
    The code you posted works perfectly fine:
    Code:
    #include <string>
    
    int main() {
        std::string *str = NULL;
        return 0;
    }
    is a marvelous piece of code that compiles with no errors.

  12. #12
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    The code you posted works perfectly fine:
    Code:
    #include <string>
    
    int main() {
        std::string *str = NULL;
        return 0;
    }
    is a marvelous piece of code that compiles with no errors.
    Well, it didn't for me. Still, multiple errors.
    Evidently, my compiler doesn't seem to like when I try to create a "new" string outside any function.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Also note that the code was only an example anyway. In the real code that I was really asking about, all variables (including the ones that are created in dynamic memory) are created inside functions.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Programmer_P View Post
    Well, it didn't for me. Still, multiple errors.
    Evidently, my compiler doesn't seem to like when I try to create a "new" string outside any function.
    No ......... You can't do anything at all outside of a function.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Dude! The clumsiest "programming ninja" ever!

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