473,466 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Array arguments

Hello.

I have a question regarding arrays when used as arguments in function
calls. In a situation like the following:

void foo(int array[2])
{
// ...
}

int bar[2] = {2, 3};
foo(bar);

What happens to the 'bar' "object" being passed to the function? I
expected it to be passed as it is, i.e., an "object" of type int [2],
without any conversion taking place. However, section 7.2.1 of
Stroustrup's TCPL 3rd Edition reads:

"If an array is used as a function argument, a pointer to its initial
element is passed."

and a bit further:

"...arrays differ from other types in that an array is not (and cannot
be) passed by value."

Does it mean that, in the sample code above, 'bar' is actually being
passed as 'int*' and then converted back to int [2] in the called
function? I am aware that the name of arrays may be used as pointers to
its first elements and that this conversion takes place automatically,
but I thought that might be different when explicitly declaring an
argument as having a type such as int [2].

Why can't arrays be passed by value? I find it somewhat inconsistent
once one could easily imply reference semantics by using pointer or
reference syntax:

void foo(int (*array)[2]) {}
void foo(int (&array)[2]) {}

I would appreciate if these issues could be elaborated a little so that
I can clear up my concepts.

Thank you,

--
Ney André de Mello Zunino
Jul 22 '05 #1
4 7249
Ney André de Mello Zunino wrote:

Why can't arrays be passed by value? I find it somewhat inconsistent
once one could easily imply reference semantics by using pointer or
reference syntax:


Besides being the standard, it takes less time to pass arrays by reference
than by value. Imagine trying to pass an array of several thousand elements
(or even 100) of type double or type string. To pass all those array values
would take a significant amount of time. IMO, it seems like a better idea
to pass the address of the first element and then index into the array to
retrieve the value of each element.
Jul 22 '05 #2
On Wed, 11 Aug 2004 20:48:15 -0300, Ney André de Mello Zunino
<zu****@inf.ufsc.br> wrote in comp.lang.c++:
Hello.

I have a question regarding arrays when used as arguments in function
calls. In a situation like the following:

void foo(int array[2])
{
// ...
}

int bar[2] = {2, 3};
foo(bar);

What happens to the 'bar' "object" being passed to the function? I
expected it to be passed as it is, i.e., an "object" of type int [2],
without any conversion taking place. However, section 7.2.1 of
Stroustrup's TCPL 3rd Edition reads:

"If an array is used as a function argument, a pointer to its initial
element is passed."

and a bit further:

"...arrays differ from other types in that an array is not (and cannot
be) passed by value."

Does it mean that, in the sample code above, 'bar' is actually being
passed as 'int*' and then converted back to int [2] in the called
function? I am aware that the name of arrays may be used as pointers to
its first elements and that this conversion takes place automatically,
but I thought that might be different when explicitly declaring an
argument as having a type such as int [2].
Use of the name 'bar' in almost all contexts results in a conversion
to a pointer to the first element of the array. Exceptions are when
applying the & and sizeof operators to the name of an array.

But inside the function, it is NOT converted back to int[2], it is
received as, is handled as, and remains a pointer to int. You may be
confused by the fact that C++ allows array subscripting notation to be
used on pointers.

For fundamental types and even user defined types where operator [] is
not overloaded, C++ inherits pointer behavior from C. Array access
such as 'bar[1]' is actually defined in pointer terms. The expression
is processed by the compiler as though it were written *(bar + 1).
Why can't arrays be passed by value? I find it somewhat inconsistent
Arrays can be passed by value. Merely place an array inside a class
or struct and pass that object by value.
once one could easily imply reference semantics by using pointer or
reference syntax:

void foo(int (*array)[2]) {}
Yes, this is a pointer to an array of two ints. It has the same
address as the array, but a different type.
void foo(int (&array)[2]) {}
Likewise, this is a reference to an array of two ints.
I would appreciate if these issues could be elaborated a little so that
I can clear up my concepts.

Thank you,


This is the way that C was originally defined, more than three decades
ago, for reasons that made sense to its designers in the context for
which it was designed at the time. For backwards compatibility with
millions of likes of existing C and C++ code, this will never change.

C++ offers alternatives to arrays, such as vectors, which can do what
you want. Or if you have a fixed-size array you can do what I
mentioned above, make it a member of a struct or class and then you
can both pass and return it by value.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #3
Jack Klein wrote:

[...]
But inside the function, it is NOT converted back to int[2], it is
received as, is handled as, and remains a pointer to int. You may be
confused by the fact that C++ allows array subscripting notation to be
used on pointers.
Thank you, Jack, for the clarifications. But, if the array argument is
received and handled as a pointer to its element type, regardless of the
way it (the argument) is declared, what are the differences/implications
of using

void foo(int array[2])

as opposed to

void foo(int* array)

?

[...]
This is the way that C was originally defined, more than three decades
ago, for reasons that made sense to its designers in the context for
which it was designed at the time. For backwards compatibility with
millions of likes of existing C and C++ code, this will never change.

C++ offers alternatives to arrays, such as vectors, which can do what
you want. Or if you have a fixed-size array you can do what I
mentioned above, make it a member of a struct or class and then you
can both pass and return it by value.


I use vectors everywhere I can, keeping the use of bare arrays to a
minimum. I was actually just experimenting with the syntax in order to
clear up some concepts. I think you have made it quite clear that the
rules involved are derived from C++'s intended commitment to having
backwards compatibility with C.

Thank you again,

--
Ney André de Mello Zunino
Jul 22 '05 #4

"Ney André de Mello Zunino" <zu****@undl.org.br> wrote in message
news:2o************@uni-berlin.de...
Jack Klein wrote:

[...]
But inside the function, it is NOT converted back to int[2], it is
received as, is handled as, and remains a pointer to int. You may be
confused by the fact that C++ allows array subscripting notation to be
used on pointers.


Thank you, Jack, for the clarifications. But, if the array argument is
received and handled as a pointer to its element type, regardless of the
way it (the argument) is declared, what are the differences/implications
of using

void foo(int array[2])

as opposed to

void foo(int* array)


There is no difference.

john
Jul 22 '05 #5

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

Similar topics

8
by: Paul | last post by:
This method: static void swap(int a, int b) { int c = a; a = b; b = c; } does not swap the values of a and b over. It doesn't work for String variables either. However the following method...
3
by: Slim | last post by:
I am trying to return a 2 dimensional array from a VB component, with no luck I get the array, it has the right Ubound for the first dimension, but 0 for the second. it works find when called...
4
by: leslie_tighe | last post by:
Hello, I have a method on a com+ object that is returning an array of objects. I know the array is popluated as calls to check the ubound and lbound show valid values. However, any calls to...
6
by: prashna | last post by:
Hi all, Is there any difference between the following 2 function prototypes which accepts an array as argument? void foo(int arr); void foo(int *arr); Which of the 2 is the best method of...
8
by: lovecreatesbeauty | last post by:
Hello experts, I have seen following the code snippet given by Marc Boyer (with slight changes by me for a better format), and have doubts on it. I am so grateful if you can give me your kindly...
6
by: main() | last post by:
I'm a newbie. These questions arose out of my curiosity, Please pardon me if this questions sound silly. 1. Why are the automatic variables are left uninitialized by default( as i understand...
12
by: einsanic | last post by:
Dear everyone, I am new to this forum and I am realitevely new to C programming so please forgive me for any basic mistakes I'll be making. I am trying to dynamically allocate the space for...
1
by: agendum97 | last post by:
MSDN says splice has the following arguments: arrayObj.splice(start, deleteCount, ]]]) Thus I can insert items into an array using splice. But how do I insert an entire array? For example:...
5
by: mahhood | last post by:
Warning: array_unshift() : The first argument should be an array in /home/mash99/public_html/thebestoptions.co.cc/book-store/finstall.php on line 383 Warning: array_unshift() : The first argument...
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
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,...
1
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.