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

reference to array?

Hope you know what I mean...
void Work(int& r[4])
{
r[0]=0;
}

int main(int, char**)
{
int a[4]
Work(a);
}

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #1
13 6160

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2s*************@uni-berlin.de...
Hope you know what I mean...
void Work(int& r[4])


void Work(int (&r)[4])

Reference to array, not array of references (which is not allowed).

john
Jul 22 '05 #2
Gernot Frisch wrote:
Hope you know what I mean...
void Work(int& r[4])


You've declared a four element array of references
rather than a refernce to array of 4 ints (which would
be int (&r)[4].

However, you don't even need that. C++ inherits the
braindamaged array passing behavior from C. A silent
conversion of the function type to pointer-to-first element
occurs.

Your code will work the way you expect if you define the
function as:
void Work(int r[4]) {

Arrays are not passed by value as would be consistant with every other
type in the language. The array r is the same as the one in the caller
here.
Jul 22 '05 #3
Gernot Frisch posted:
Hope you know what I mean...
void Work(int& r[4])
{
r[0]=0;
}

int main(int, char**)
{
int a[4]
Work(a);
}


Here's some functions that'll alter an array of 4 elements:

void Monkey(int* const p_blah) throw()
{
p_blah[0] = 67;
p_blah[1] = -54;
p_blah[2] = 76;
p_blah[3] = 47;
}

void Ape(int blah[4]) throw() //Looks like it's passing by value...
{
blah[0] = 67;
blah[1] = -54;
blah[2] = 76;
blah[3] = 47;
}
void Cow(int (&blah)[4]) throw()
{
blah[0] = 67;
blah[1] = -54;
blah[2] = 76;
blah[3] = 47;
}
-JKop
Jul 22 '05 #4
> Your code will work the way you expect if you define the
function as:
void Work(int r[4]) {


How can I happen to be so silly to have forgotten that... Thank you
very much.
Jul 22 '05 #5
>Your code will work the way you expect if you define the
function as:
void Work(int r[4]) {


But that would allow passing the address of any int to the function be it a
single element or an array of any size.

Maybe the function is hardcoded to work with arrays of a specific size.

It seems the reference syntax would prevent this kind of usage...

void func(int (&r)[3])
{
}

int main()
{
int *x;
func(x); //Sorry

int y[2];
func(y); //Nope

return 0;
}

Do you see any value in the "reference to array" syntax?

I have never needed it, but am curious to peoples opinions.

Jul 22 '05 #6
"JKop" <NU**@NULL.NULL> wrote in message
news:64*******************@news.indigo.ie...
Gernot Frisch posted:
Hope you know what I mean...
void Work(int& r[4])
{
r[0]=0;
}

int main(int, char**)
{
int a[4]
Work(a);
}


Here's some functions that'll alter an array of 4 elements:

void Monkey(int* const p_blah) throw()
{
p_blah[0] = 67;
p_blah[1] = -54;
p_blah[2] = 76;
p_blah[3] = 47;
}


I think const is a little misleading here. The function gets a copy of the
pointer anyway so the caller wouldn't care if the function changed its
value. Also, I see you have discovered throw specifications. Unfortunately
they don't do what you might think (for instance prevent the function from
throwing) and I think most experienced programmers avoid them.

[snip]

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #7
void Monkey(int* const p_blah) throw()
{
p_blah[0] = 67;
p_blah[1] = -54;
p_blah[2] = 76;
p_blah[3] = 47; }

I think const is a little misleading here.


I disagree. It's crystal clear to me that "p_blah" is a local object of type
"int*" and that it's const. Sure, it's not needed, but I use "const"
wherever possible and it allows for more obvious optimization.
The function gets a copy of
the pointer anyway so the caller wouldn't care if the function changed
its value.
Agreed.
Also, I see you have discovered throw specifications.
Indeed.
Unfortunately they don't do what you might think (for instance prevent
the function from throwing) and I think most experienced programmers
avoid them.


I know exactly how it works. If this particular function threw an exception,
then the function "std::unexpected" would be called.

The reason I stick it in is that I *know* that my function won't ever throw
an exception, and as with "const", I'll use it wherever possible.

I wouldn't though for instance do the following:

void blah()
{
string k("salj");
}
Because I don't know the insides of that type and it may very well throw an
exception I'm unaware of.
-JKop
-JKop
Jul 22 '05 #8
void blah()
{
string k("salj");
}

Should've been:
void blah() throw()
{

string k("salj");
}
-JKop
Jul 22 '05 #9
In message <c_*******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
void blah()
{
string k("salj");
}

Should've been:
void blah() throw()
{

string k("salj");
}


Shouldn't.

What do you think happens if the string constructor can't allocate
enough memory?

--
Richard Herring
Jul 22 '05 #10
Richard Herring posted:
In message <c_*******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
void blah()
{
string k("salj"); }

Should've been:
void blah() throw()
{

string k("salj"); }


Shouldn't.

What do you think happens if the string constructor can't allocate
enough memory?

Asshole, please read both my posts, then and only then return with your
usual asshole remarks.
-JKop
Jul 22 '05 #11
In message <Qx*******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
Richard Herring posted:
In message <c_*******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes

void blah()
{
string k("salj"); }
Should've been:
void blah() throw()
{

string k("salj"); }

Shouldn't.

What do you think happens if the string constructor can't allocate
enough memory?

Asshole, please read both my posts,


Life's too short.

If you can't take the trouble to say what you mean the first time round,
you need to make sure your corrections contain enough context to make
your point clear. Otherwise, expect people reading posts in isolation
to take them at face value.
then and only then return with your
usual asshole remarks.


If you don't like them, there are obvious solutions.

--
Richard Herring
Jul 22 '05 #12
"JKop" <NU**@NULL.NULL> wrote in message
news:c_*******************@news.indigo.ie...
void blah()
{
string k("salj");
}

Should've been:
void blah() throw()
{

string k("salj");
}
-JKop


Tell it to Herb:

http://www.gotw.ca/publications/mill22.htm

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #13
Cy Edmunds posted:
http://www.gotw.ca/publications/mill22.htm


Interesting! Thanks.
-JKop
Jul 22 '05 #14

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

Similar topics

5
by: Nico | last post by:
Hello folks, I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist = new item("myitem"); //... and I am looking to develop a search function, which...
6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
3
by: Ohmu | last post by:
Hi! How to pass an (multidimensional)array of something to a function with reference/pointer? Can anyone help me with that? Thanks, Ohmu
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
7
by: rossum | last post by:
Is there any way of creating a read only reference to an array? My class includes a private array of bytes with a Property to access it. However, I do not want users to use the returned reference...
11
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the...
9
by: Jack | last post by:
If I don't specify "ref" in the argument list when passing an array to the callee, I am passing the array (reference) by value. But this makes me confused because it actually means a "reference" of...
7
by: arnuld | last post by:
this programme gives unusual output. i am not able to find out where the semantic bug lies: /* C++ Primer - 4/e * * an example from section section 7.2.4, page 241 * STATEMENT * write a...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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:
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: 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...
0
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,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.