473,241 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

anonymous array of strings // ("taking address of temporary"- how long is temporary valid?)

Hello!

1) ===============================
When trying to define an array of std::string ...

func( (std::string []) { std::string("ab"), std::string("cd"),
std::string("ef") } , 3 );

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
....g++ tells me: "invalid use of non-lvalue array"
Can the above be modified to work?

void func(std::string arr[], int n)
{
while ((--n) >= 0) {
std::cout << arr[n];
}
}
2)================================

When trying to work with an array of std::string-pointers ...

func2( (std::string* []) { &std::string("ab"), &std::string("cd"),
&std::string("ef") } , 3 );
//
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
// ~~ nextline

.... g++ reports:
"taking address of temporary" (reported 3 times)
"invalid use of non-lvalue array".

When are the 3 temporary addresses in danger of being "corrupted"? Is
it during the execution of func2, or is it only after func2, i.e.
at // ~~ nextline

How can the error be fixed?

void func2(std::string* arr[], int n)
{

// possible memory allocations etc...

while ((--n) >= 0) {
std::cout << *(arr[n]);
}
}

Thanks for advice!
-Albert
Feb 11 '08 #1
2 2338
On Feb 11, 7:06 am, anon.a...@gmail.com wrote:
Hello!

1) ===============================
When trying to define an array of std::string ...

func( (std::string []) { std::string("ab"), std::string("cd"),
std::string("ef") } , 3 );
Perl programmer are we? :-)

Thats hard to read and I am not sure it would be valid syntax. It
looks like a comma expression to me, I don't think you can use the
array initializer like that (somebody with a copy of the standard will
give you the exact problem). Also the fact that you are using a C
style cast is an indication that something is not good with the code.

A simple solution would be:

std::string[] temp = {"ab","cd","ef"};
func(temp,3);

func2( (std::string* []) { &std::string("ab"), &std::string("cd"),
&std::string("ef") } , 3 );
//
... g++ reports:
"taking address of temporary" (reported 3 times)
"invalid use of non-lvalue array".

Temporary objects are constant and only last as long as the statement.
Thus taking there address is probably not a good idea that is why the
compiler is generating a "WARNING". Also again I don't think you are
actually creating an array. Using a cast like that forces the type to
be something without checking. The compiler is not adding any code to
do the conversion.

When are the 3 temporary addresses in danger of being "corrupted"? Is
it during the execution of func2, or is it only after func2, i.e.
at // ~~ nextline
The temporaries are valid until the end of the statement they are
created in (except in funny situations that don't arise here). So they
will be valid until after func2() returns.


Feb 11 '08 #2
On 11 Feb., 16:06, anon.asdf wrote:
Hello!

1) ===============================
When trying to define an array of std::string ...

func( (std::string []) { std::string("ab"), std::string("cd"),
std::string("ef") } , 3 );

void func(std::string arr[], int n)
{
while ((--n) >= 0) {
std::cout << arr[n];
}

}
Interestingly, the following works fine:

//===================================
#include <iostream>

void func3(char* arr[], int n);

int main(void)
{
func3((char* []){"ab", "cd", "ef"}, 3);
return 0;
}

void func3(char* arr[], int n)
{
while ((--n) >= 0) {
std::cout << arr[n];
}
std::cout << std::endl;
}
//===================================

Can it be similarly adapted for std::string, without naming an array
variable?

-Albert
Feb 12 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: MLH | last post by:
Gentlemen: I have declared an array Dim MyTables(14) AS Long Now I want to assign values for MyTables(0) - MyTables(14) equal to the number of records in each table. Catch, I want the code...
24
by: Don | last post by:
Hi I have an array of unsigned chars, like: MyArray = {0x00}; For memory-mapping purposes I need to store this array at a specific address (0xFFFF1199) How do I declare this? I cant't do...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
6
by: Anders Thomsen | last post by:
Hi, Is it possible on VB.NET to create anonymous arrays, e.g. passing arrays directly as a method-parameter instead of reference it from a variable. In C#, I can do this: string s = "Blah...
65
by: kyle.tk | last post by:
I am trying to write a function to convert an ipv4 address that is held in the string char *ip to its long value equivalent. Here is what I have right now, but I can't seem to get it to work. ...
10
by: Krustov | last post by:
$rambo="daffy duck"; $rambo="mad max"; $rambo="daffy duck"; $rambo="superman"; etc etc How do i remove duplicate strings from a array ? . ('daffy duck' could appear more than twice in the...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
1
by: dvijayan | last post by:
Hi , How to get the length of Anonymous array. In my code sometimes the Anonymous array is becoming empty want to go a head only if the array is not empty...how to do?? Thanks
3
by: ejaggers | last post by:
I'm teaching myself OOP by following examples. I can't figure out what this is doing for the life of me. I've read the explanation and still don't get it. Can anyone explain, plus is there a more...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.