Immediate arrays

Discussion in 'C++' started by Paul N, Sep 9, 2011.

  1. Paul N

    Paul N Guest

    Is there a way to have an array temporarily without giving it a name?
    For instance, can you do something along the lines of:

    char *str;

    str = { "Tom", "Dick", "Harry" } [2];

    Many thanks.
    Paul.
    Paul N, Sep 9, 2011
    #1
    1. Advertising

  2. On 9/8/2011 7:08 PM, Paul N wrote:
    > Is there a way to have an array temporarily without giving it a name?
    > For instance, can you do something along the lines of:
    >
    > char *str;
    >
    > str = { "Tom", "Dick", "Harry" } [2];


    No. Why would you need that? Why not simply

    const char* str;
    ...
    str = "Harry";

    ?

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 9, 2011
    #2
    1. Advertising

  3. Paul N

    SG Guest

    On 9 Sep., 01:08, Paul N wrote:
    > Is there a way to have an array temporarily without giving it a name?
    > For instance, can you do something along the lines of:
    >
    > char *str;
    >
    > str = { "Tom", "Dick", "Harry" } [2];


    char const* func(unsigned index) {
    assert(index<3);
    return "Tom\0 "
    "Dick\0"
    "Harry"+(index*5);
    }

    :p

    But seriously, don't do that.

    Cheers!
    SG
    SG, Sep 9, 2011
    #3
  4. On 9/8/2011 4:08 PM, Paul N wrote:
    > Is there a way to have an array temporarily without giving it a name?
    > For instance, can you do something along the lines of:
    >
    > char *str;
    >
    > str = { "Tom", "Dick", "Harry" } [2];
    >


    The feature exists in C99 version of C, but not in standard C++

    // C99 compound literal
    const char *str = (const char*[]) { "Tom", "Dick", "Harry" } [2];

    So, your only hope is that your compiler borrows it from C as an extension.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Sep 9, 2011
    #4
  5. Paul N wrote:

    > Is there a way to have an array temporarily without giving it a name?
    > For instance, can you do something along the lines of:
    >
    > char *str;
    >
    > str = { "Tom", "Dick", "Harry" } [2];
    >


    Yes, but you need a utility first to create it on demand:

    template<typename T> using id = T;

    So then it looks natural

    str = id<char const*[]>{"Tom", "Dick", "Harry"}[2];

    Hope it helps.
    Johannes Schaub, Sep 9, 2011
    #5
  6. Johannes Schaub wrote:

    > Paul N wrote:
    >
    >> Is there a way to have an array temporarily without giving it a name?
    >> For instance, can you do something along the lines of:
    >>
    >> char *str;
    >>
    >> str = { "Tom", "Dick", "Harry" } [2];
    >>

    >
    > Yes, but you need a utility first to create it on demand:
    >
    > template<typename T> using id = T;
    >
    > So then it looks natural
    >
    > str = id<char const*[]>{"Tom", "Dick", "Harry"}[2];
    >
    > Hope it helps.


    But note: as the array is a temporary, the pointer "str" will point to
    unallocated memory after that assignment expression has been evaluated,
    because at the end of a full expression, all temporaries not directly bound
    by references will be destroyed.
    Johannes Schaub, Sep 9, 2011
    #6
  7. Paul N

    SG Guest

    On 9 Sep., 23:41, Johannes Schaub wrote:
    >
    > > Yes, but you need a utility first to create it on demand:
    > >     template<typename T> using id = T;
    > > So then it looks natural
    > >     str = id<char const*[]>{"Tom", "Dick", "Harry"}[2];
    > > Hope it helps.

    >
    > But note: as the array is a temporary, the pointer "str" will point to
    > unallocated memory after that assignment expression has been evaluated,


    I don't think so. The array of pointers will vanish, but the arrays of
    characters will stay. string literals are stored in static memory, are
    they not?

    SG
    SG, Sep 11, 2011
    #7
  8. SG wrote:

    > On 9 Sep., 23:41, Johannes Schaub wrote:
    >>
    >> > Yes, but you need a utility first to create it on demand:
    >> > template<typename T> using id = T;
    >> > So then it looks natural
    >> > str = id<char const*[]>{"Tom", "Dick", "Harry"}[2];
    >> > Hope it helps.

    >>
    >> But note: as the array is a temporary, the pointer "str" will point to
    >> unallocated memory after that assignment expression has been evaluated,

    >
    > I don't think so. The array of pointers will vanish, but the arrays of
    > characters will stay. string literals are stored in static memory, are
    > they not?
    >


    Yes, it was my mistake. What you say is right. I canceled the above note a
    few minutes after I sent the message, but apparently it wasn't removed from
    all usenet servers.
    Johannes Schaub, Sep 11, 2011
    #8
    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. =?Utf-8?B?TW9oaXQgR3VwdGE=?=

    ASP .NET and ASP style confusion. Immediate solution required

    =?Utf-8?B?TW9oaXQgR3VwdGE=?=, Oct 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    796
    Scott Allen
    Oct 18, 2004
  2. TR
    Replies:
    0
    Views:
    554
  3. Timo
    Replies:
    0
    Views:
    382
  4. Timo
    Replies:
    2
    Views:
    2,822
  5. Philipp
    Replies:
    21
    Views:
    1,109
    Philipp
    Jan 20, 2009
Loading...

Share This Page