Results 1 to 3 of 3

Thread: String array...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    String array...

    How do I create a string array? I know that the individual strings are 8 characters long, but I don't know how many of them I will have...

    thnx,
    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  2. #2
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    Use a:

    Code:
    // 8 characters long - dont know how many
    char *myString[8];
    
    // know how many - stored in intHoweverMany!
    *myString = (char**)malloc(sizeof(char[8]) * intHoweverMany);
    Something like that.

    HD

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    One syntax error. And don't forget to free that. And it should be char[9] for the terminating NUL.

    C++:
    Code:
    // 8 characters long - dont know how many
    char *myString[9];
    
    // know how many - stored in intHoweverMany!
    myString = new char[9][intHoweverMany];
    
    // ...
    delete[] myString;
    Better C++:
    Code:
    #include <string>
    using namespace std;
    
    string *myString;
    
    myString = new string[numStrings];
    
    // ...
    delete[] myString;
    And if the number of strings could change afterwards (or you have to just read string by string and don't know when you come to the end):
    Code:
    // This line is only for MSVC++
    #pragma warning(disable: 4786)
    
    #include <vector>
    #include <string>
    using namespace std;
    
    typedef vector<string> strvec;
    
    strvec myString;
    
    // e.g.
    myString.push_back("Hello, World!");
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width