473,769 Members | 7,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::strin g 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("a b"), &std::string("c d"),
&std::string("e f") } , 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::stri ng* arr[], int n)
{

// possible memory allocations etc...

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

Thanks for advice!
-Albert
Feb 11 '08 #1
2 2373
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("a b"), &std::string("c d"),
&std::string("e f") } , 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::strin g 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
1843
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 to identify only those tables beginning with the letters "JA". Is this possible? Or, should I break down and manually type in all the table names?
24
16332
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 MyArray = {0x00} @ 0xFFFF1199 :-(
9
2316
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
7509
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 blah, Aloha!"; string sArr = s.Split(new String{","});
65
21473
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. #include <string.h> #include <stdio.h> /* Convert an ipv4 address to long integer */ /* "192.168.1.1" --> 3232235777 */ unsigned long iptol(char *ip){
10
2091
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 array)
7
2813
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 naming names here to remove bias - I'm trying to tell if I'm relying on implementation defined behavior or if this is a bug in the new compiler. Consider this stripped down example:
1
3458
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
1500
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 obvious way to do this: MY CONFUSION IS THIS STEP: push (@{$year_index {$year}}, $rlEntry); # By Year BOOK'S EXPLANATION: Since we would like to retrieve entries by category or by year, we use a double indexing scheme. Notice that...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.