CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Posts
    24

    Can you have an array of strings?

    string [2];
    does this create two strings or a string of 2 characters?
    If the latter how do you make it work as an array of strings?
    Last edited by E-man96; January 30th, 2009 at 05:14 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Can you have an array of strings?

    Probably neither. This declares an array, named strings, of two std::string objects:
    Code:
    std::string strings[2];
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Can you have an array of strings?

    It depends what the code is that you posted - laserlight has most likely given you the most appropriate answer.

    But anyhow just posting

    Code:
    string [2];
    isn't the most helpful code snippet - for one thing, it is not valid code from a declaration point of view. Therefore it could mean more than one thing. For example, it could be the use of operator[]. e.g.

    Code:
    const char* string = "Hello world";
    
    std::cout << string[2] << std::endl; //outputs the letter 'l'
    Hence, a little more context (like posting the whole line of code) would be useful.

  4. #4
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    Re: Can you have an array of strings?

    Why to have an array of 2 string ? ( Something not good there)
    Why not to have array with pointers to those strings ?

    (Missunderstud question )
    Last edited by ulumulu; January 30th, 2009 at 04:26 PM.
    Share and always try to give back more.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Can you have an array of strings?

    I don't see why pointers should get involved here. They certainly don't have to.

  6. #6
    Join Date
    May 2007
    Posts
    811

    Re: Can you have an array of strings?

    Just in case you might need different number of strings
    Code:
    std::vector<std::string>

Tags for this Thread

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

Featured