Thread: String Array

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    27

    String Array

    Hey,

    How do I make a string array like

    string blah[10];


    blah[0]="blibity blah";
    blah[1]="blibbering baww";


    Thanks,

    Gr3g
    Chance favors the prepared mind.

    Vis. C++ 6.0

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Hi Gr3g,

    You need to copy the strings to your array:
    Code:
    char blah[2][100];  /* can hold 2 strings, each max 99 characters long */
    
    strcpy(blah[0], "blibity blah");
    strcpy(blah[1], "blibbering baww");
    or if you are using MFC:
    Code:
    CString blah[2];
    
    blah[0] = "blibity blah";
    blah[1] = "blibbering baww";

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    That is right ...

    Hi,
    I just want to add something ....

    Every string is an array of char, has the same number of char that the string has + "/0", which will allow it to have one more room for the " /0" { "/0 means end of hte string "}

    Now you can do what monster told you to do, or you can use dinamic array,

    As in the same above,,,!

    C++
    The best

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    See ... I tried it ...

    it won't give you any kind of misstake...
    you better be carful next time...

    I hope that helped you ....
    C++
    The best

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    27

    Talking :)

    Yes thank you, Im going to write my program now, when (hehe or if) i finish ill post the code...


    Gr3g
    Chance favors the prepared mind.

    Vis. C++ 6.0

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Or...
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        string blah[2] = { "blibity blah", "blibbering baww" };
    
        cout << blah[0] << ' ' << blah[1] << endl;
    
        return 0;
    }
    Prints out: blibity blah blibbering baww
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM