Immediate arrays

P

Paul N

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.
 
V

Victor Bazarov

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
 
S

SG

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
 
A

Andrey Tarasevich

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.
 
J

Johannes Schaub

Paul said:
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.
 
J

Johannes Schaub

Johannes said:
Paul said:
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.
 
S

SG

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
 
J

Johannes Schaub

SG said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top