CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2002
    Location
    Ballymena
    Posts
    57

    Character Arrays

    Hi,
    I need to have an array so as to look like the following:

    ArrayElement[1] = "First ELement";
    ArrayElement[2] = "Second Element";

    etc...

    How do I declare this?

    ie Is it
    char* ArrayElement[3];

    Or how is it declared?

    Thanks in advance,
    Jenny

  2. #2
    Join Date
    May 2001
    Posts
    472
    To declare an array of strings, allocate the space for the largest string:
    Code:
    char ArrayElement[3][15];
    
    strcpy(ArrayElement[0], "First ELement");
    strcpy(ArrayElement[1], "Second Element"); // 14 chars + '\0'
    strcpy(ArrayElement[2], "Last Element");
    Ce n'est que pour vous dire ce que je vous dis.

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    What are you trying to do?

    -----
    char *pArray = new char[SIZE];
    -----

    Is that what you want to do?

    Kuphryn

  4. #4
    Join Date
    May 2002
    Location
    Ballymena
    Posts
    57

    Talking

    Hi Guys!

    Thanks for the answers

    Alexey, It was exactly what I needed, Thanks!


  5. #5
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    You could also use
    Code:
    const char* pArray[] =
    {
        "First Element",
        "Second Element",
        "Third Element"
    };
    Then there is no need to figure out how many you have or the size of the largest string.
    Succinct is verbose for terse

  6. #6
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Just because it should be said:

    An array of std::string would be better.

    A std::vector of std::string would be even better.

    Jeff

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Well said, that man!
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


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