473,382 Members | 1,313 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,382 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 2346
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.