Strings inside arrays

Discussion in 'C++' started by LinuxDuud, Jan 16, 2007.

  1. LinuxDuud

    LinuxDuud Guest

    Hi,
    First, Sorry if the title was bad, I really didn't know how to describe
    this.
    So,
    What I need to do is something like this:
    char somevariable[256]; // The tricky part. I don't know how to define
    it.
    somevariable[1] = "Hello";
    somevariable[2] = "Goodbye";
    And to print them using:
    printf(somevariable[1]);
    and
    printf(somevariable[2]);

    Hope I was describing this enough.
    Thanks, LinuxDuud.
    LinuxDuud, Jan 16, 2007
    #1
    1. Advertising

  2. LinuxDuud

    Mike Wahler Guest

    "LinuxDuud" <> wrote in message
    news:...
    > Hi,
    > First, Sorry if the title was bad, I really didn't know how to describe
    > this.
    > So,
    > What I need to do is something like this:
    > char somevariable[256]; // The tricky part. I don't know how to define
    > it.
    > somevariable[1] = "Hello";
    > somevariable[2] = "Goodbye";
    > And to print them using:
    > printf(somevariable[1]);
    > and
    > printf(somevariable[2]);
    >
    > Hope I was describing this enough.
    > Thanks, LinuxDuud.


    #include <iostream>
    #include <string>
    #include <vector>

    using std::cout;
    using std::string;
    using std::vector;

    int main()
    {
    vector<string> text;

    text.push_back("Hello");
    text.push_back("The C++ standard library has many easy to use,");
    text.push_back("robust and versatile types and algorithms. Use them.");
    text.push_back("Goodbye");

    for(vector<string>::size_type i = 0; i < text.size(); ++i)
    cout << text << '\n';

    return 0;
    }

    -Mike
    Mike Wahler, Jan 16, 2007
    #2
    1. Advertising

  3. LinuxDuud

    Jack Klein Guest

    On 16 Jan 2007 09:56:29 -0800, "LinuxDuud" <> wrote in
    comp.lang.c++:

    > Hi,
    > First, Sorry if the title was bad, I really didn't know how to describe
    > this.
    > So,
    > What I need to do is something like this:
    > char somevariable[256]; // The tricky part. I don't know how to define
    > it.
    > somevariable[1] = "Hello";
    > somevariable[2] = "Goodbye";
    > And to print them using:
    > printf(somevariable[1]);
    > and
    > printf(somevariable[2]);
    >
    > Hope I was describing this enough.
    > Thanks, LinuxDuud.


    Somebody else already showed you a way to use C++ vectors and streams
    to do what you want, completely ignoring the fact that you might have
    a good reason to use C style strings and the printf() function. And
    you should prefer vectors unless there is some reason that you cannot.

    But to answer your original question:

    const char *somevariable [256] =
    {
    "Hello",
    "Goodbye",
    /* up to 254 more string literals */
    };

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jan 16, 2007
    #3
  4. LinuxDuud

    Noah Roberts Guest

    Mike Wahler wrote:
    > "LinuxDuud" <> wrote in message
    > news:...
    > > Hi,
    > > First, Sorry if the title was bad, I really didn't know how to describe
    > > this.
    > > So,
    > > What I need to do is something like this:
    > > char somevariable[256]; // The tricky part. I don't know how to define
    > > it.
    > > somevariable[1] = "Hello";
    > > somevariable[2] = "Goodbye";
    > > And to print them using:
    > > printf(somevariable[1]);
    > > and
    > > printf(somevariable[2]);
    > >
    > > Hope I was describing this enough.
    > > Thanks, LinuxDuud.

    >
    > #include <iostream>
    > #include <string>
    > #include <vector>
    >
    > using std::cout;
    > using std::string;
    > using std::vector;
    >
    > int main()
    > {
    > vector<string> text;
    >
    > text.push_back("Hello");
    > text.push_back("The C++ standard library has many easy to use,");
    > text.push_back("robust and versatile types and algorithms. Use them.");
    > text.push_back("Goodbye");
    >
    > for(vector<string>::size_type i = 0; i < text.size(); ++i)
    > cout << text << '\n';


    With such a message I would think your loop might look more like this:

    std::copy(text.begin(), text.end(),
    std::eek:stream_iterator<std::string>(cout, "\n"));

    >
    > return 0;
    > }
    >
    > -Mike
    Noah Roberts, Jan 16, 2007
    #4
  5. LinuxDuud

    Mike Wahler Guest

    "Noah Roberts" <> wrote in message
    news:...
    >
    > Mike Wahler wrote:


    >>
    >> for(vector<string>::size_type i = 0; i < text.size(); ++i)
    >> cout << text << '\n';

    >
    > With such a message I would think your loop might look more like this:
    >
    > std::copy(text.begin(), text.end(),
    > std::eek:stream_iterator<std::string>(cout, "\n"));


    :)
    Actually, I indeed almost did exactly that.

    But I thought better of it, since OP seems to obviously
    be a C++ beginner. I Didn't want to overload (pun intended)
    him with too much info right away.

    -Mike
    Mike Wahler, Jan 16, 2007
    #5
  6. LinuxDuud

    Grizlyk Guest

    Jack Klein wrote:

    > Somebody else already showed you a way to use C++ vectors and streams
    > to do what you want, completely ignoring the fact that you might have
    > a good reason to use C style strings and the printf() function. And
    > you should prefer vectors unless there is some reason that you cannot.


    I think so, that printf() can be used in some cases if you well know
    the function, but you can get wrong output, if you will mix
    "std::stdout" and "std::cout" streams. To avoid backside effects use
    "printf" and "<<" into separate streams, for example, do printf into
    stderror or file and << into cout.
    Grizlyk, Jan 17, 2007
    #6
  7. LinuxDuud

    Lionel B Guest

    On Tue, 16 Jan 2007 21:19:35 -0800, Grizlyk wrote:

    > Jack Klein wrote:
    >
    >> Somebody else already showed you a way to use C++ vectors and streams
    >> to do what you want, completely ignoring the fact that you might have
    >> a good reason to use C style strings and the printf() function. And
    >> you should prefer vectors unless there is some reason that you cannot.

    >
    > I think so, that printf() can be used in some cases if you well know
    > the function, but you can get wrong output, if you will mix
    > "std::stdout" and "std::cout" streams. To avoid backside effects


    ....please adjust your diet ;) Seriously though, not quite sure what you
    intended there.

    > use "printf" and "<<" into separate streams, for example, do printf into
    > stderror or file and << into cout.


    Is that necessary? I think a call to std::ios_base::sync_with_stdio(true)
    should synchronise the standard C I/O facilities (stdout, etc.) with their
    standard C++ equivalents (std::cout, etc.).

    (Indeed it used to be [still is?] recommended that if you are using one or
    the other of C or C++ I/O facilities then it might be a good idea to call
    sync_with_stdio(false), since synching I/O modes might entail a substantial
    speed/size overhead).

    --
    Lionel B
    Lionel B, Jan 17, 2007
    #7
  8. LinuxDuud

    Old Wolf Guest

    Lionel B wrote:
    > Is that necessary? I think a call to std::ios_base::sync_with_stdio(true)
    > should synchronise the standard C I/O facilities (stdout, etc.) with their
    > standard C++ equivalents (std::cout, etc.).


    They are sync'd by default; you would only call that function if you
    wanted to unsync them.
    Old Wolf, Jan 17, 2007
    #8
  9. LinuxDuud

    Lionel B Guest

    On Wed, 17 Jan 2007 14:12:29 -0800, Old Wolf wrote:

    > Lionel B wrote:
    >> Is that necessary? I think a call to std::ios_base::sync_with_stdio(true)
    >> should synchronise the standard C I/O facilities (stdout, etc.) with their
    >> standard C++ equivalents (std::cout, etc.).


    Yep, sorry, that's the wrong way round...

    > They are sync'd by default; you would only call that function if you
    > wanted to unsync them.


    For that reason it was often recommended that you explicitly *un*-sync them
    with std::ios_base::sync_with_stdio(false) for better I/O performance if
    you were not intending to mix C and C++ style I/O in the same program.

    --
    Lionel B
    Lionel B, Jan 18, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,608
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,254
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    566
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    757
    Malcolm
    Jun 24, 2006
  5. Philipp
    Replies:
    21
    Views:
    1,127
    Philipp
    Jan 20, 2009
Loading...

Share This Page