473,378 Members | 1,317 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,378 software developers and data experts.

Trying to return a reference...

char (&)[6] GetArray()
{
static char hello[] = "hello";

return array;
}

int main()
{
char (&hello)[6] = GetArray();
}
Won't compile. Is there anyway around this?
-Tomás
Mar 3 '06 #1
5 1492
"Tomás" <NU**@NULL.NULL> wrote in message
news:un******************@news.indigo.ie...
char (&)[6] GetArray()
char (&GetArray())[6]
{
static char hello[] = "hello";

return array;
return hello;
}

int main()
{
char (&hello)[6] = GetArray();
}
Won't compile. Is there anyway around this?
-Tomás

Mar 3 '06 #2
Tomás wrote:
char (&)[6] GetArray()
{
static char hello[] = "hello";

return array;
}

int main()
{
char (&hello)[6] = GetArray();
}
Won't compile. Is there anyway around this?


Should it be

char ( & GetArray() )[6]
{
static char hello[6] = "Hello";
return hello;
}

You need to learn to read (and form) declarations.

V
--
Please remove capital As from my address when replying by mail
Mar 3 '06 #3
Victor Bazarov posted:
Tomás wrote:
char (&)[6] GetArray()
{
static char hello[] = "hello";

return array;
}

int main()
{
char (&hello)[6] = GetArray(); }
Won't compile. Is there anyway around this?


Should it be

char ( & GetArray() )[6]
{
static char hello[6] = "Hello";
return hello;
}

You need to learn to read (and form) declarations.

Now I've seen everything! I was thinking in my head of all the different
syntaxes it could be, maybe:

char (&) GetArray()[6];

or maybe:

( char & ) GetArray()[6];

or maybe even:

char (&GetArray)()[6];

But the one you suggested, i.e. the correct one, looked way too weird for me
to even fathom it!
-Tomás

Mar 4 '06 #4
> Now I've seen everything! I was thinking in my head of all the different
syntaxes it could be, maybe:

char (&) GetArray()[6];

or maybe:

( char & ) GetArray()[6];

or maybe even:

char (&GetArray)()[6];

But the one you suggested, i.e. the correct one, looked way too weird for me
to even fathom it!
-Tomás


Alternatively, you can resolve to using a simple template:

template <typename T>
struct type
{
typedef T self;
typedef T& ref;
typedef T* ptr;

//...
};

type<char[6]>::ref GetArray(void)
{
static char hello[] = "hello";
return hello;
}

I personally prefer this form because it is easier to read.

Ben
Mar 4 '06 #5
benben posted:
Now I've seen everything! I was thinking in my head of all the
different syntaxes it could be, maybe:

char (&) GetArray()[6];

or maybe:

( char & ) GetArray()[6];

or maybe even:

char (&GetArray)()[6];

But the one you suggested, i.e. the correct one, looked way too weird
for me to even fathom it!
-Tomás


Alternatively, you can resolve to using a simple template:

template <typename T>
struct type
{
typedef T self;
typedef T& ref;
typedef T* ptr;

//...
};

type<char[6]>::ref GetArray(void)
{
static char hello[] = "hello";
return hello;
}

I personally prefer this form because it is easier to read.

Ben

Is there an archive anywhere for little gems like this? I think there
should be! Put stuff like "DefaultInitialised" and "GetArrayLength" in...

-Tomás
Mar 4 '06 #6

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

Similar topics

5
by: John T | last post by:
I am trying to make a function that takes an optional parameter that gets passed by reference. Here is the first line of my function definition: function funQueryDatabase($strQuery,...
5
by: Weston C | last post by:
I'm trying to write a generic/reusable form validator in Javascript... just something that checks to make sure required fields have a value. By generic I mean I don't want to explicitly reference...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
4
by: Deniz Bahar | last post by:
Hello, A couple days ago my friend (OOP guy) shows me what OOP was all about in C++. This morning I figured I can do pretty much the same thing with C (by putting function pointers in...
4
by: Eric A. Johnson | last post by:
For the following code: ' return String representation of CTriangleShape Public Overrides Function ToString() As String ' use MyBase reference to return CShape String Return...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
5
by: comp.lang.php | last post by:
// NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA $_POST if ($_POST && (!is_array($leaseObj->errorArray)...
7
by: WannabePrgmr | last post by:
What I am trying to do is on the click event of "Command167", run a Dlookup on the number that was just typed into "cboMoveTo1" and find the value located in the table "tblName" in the "Open/Closed"...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.