Hi,
I am just learning about the array/pointer duality in C/C++. I couldn't
help wondering, is there a way to pass an array by value? It seems like
the only way to do is to pass it by reference??
Thanks,
BB 41 8338
Berk Birand wrote: Hi,
I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by reference??
Thanks, BB
When an array is passed in either C or C++ (different, oft subtly
incompatible languages with a common subset), it decays into a pointer
to its first element; the pointer is passed by value.
The only way to simulate passing an array by value would be to wrap it
in a struct.
HTH,
--ag
--
Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays
Do not use an array - use std::vector instead.
"Berk Birand" <gr******@yahoo .com> skrev i en meddelelse
news:1107492506 .5f08d4c39f8943 27dfa88bc7c283a 9ce@teranews... Hi,
I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by reference??
Also do not use pointers if you can avoid them. Thanks, BB
Peter Koch Larsen wrote: Do not use an array - use std::vector instead. "Berk Birand" <gr******@yahoo .com> skrev i en meddelelse news:1107492506 .5f08d4c39f8943 27dfa88bc7c283a 9ce@teranews...
Hi,
I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by reference??
Also do not use pointers if you can avoid them.
Thanks, BB
Thanks to both for your answers. Also another question that pops in my
mind is whether to use references or pointers in function headers. Is it
better practice to declare a function as, say:
void afunc(int& i);
or is it better to declare it as
void afunc(int* i);
I know that in the second case, one has to explicitly dereference i
whenever one wants to access it. But this can be considered a good
thing, as it makes it more obvious that you are manipulating pointers,
and the values are going to change. On the other hand, it's just more work!
So, what do you say about this?
Berk Birand wrote: I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, "Is there a way to pass an array by value?" It seems like the only way to do is to pass it by reference?
Actually, you pass a pointer to the first element of the array.
The only way to pass an array by value is to encapsulate it in a struct:
#include <iostream>
struct Array {
int A[10];
};
void f(Array a) {
int* A = a.A;
for (size_t j = 0; j < 10; ++j)
std::cout << ' ' << A[j];
std::cout << std::endl;
}
Berk Birand wrote: Also another question that pops in my mind mind is whether to use references or pointers in function headers. Is it better practice to declare a function as, say:
void afunc(int& i);
Or is it better to declare it as
void afunc(int* i);
I know that, in the second case, one has to explicitly dereference i whenever one wants to access it. But this can be considered a good thing, as it makes it more obvious that you are manipulating pointers, and the values are going to change. On the other hand, it's just more work!
Avoid functions that modify their arguments.
Pass a const reference and return a result by value. Write
int afunct(const int& i);
or
int afunct(const int* p);
instead. I pass small objects by value
int afunct(int i);
but you can easily substitute a function declaration/definition
that passes a const reference
in afunct(const int& i);
without breaking any applications that call afunct.
You need only recompile and relink. The only way to pass an array by value is to encapsulate it in a
struct: #include <iostream>
struct Array { int A[10]; };
void f(Array a) { int* A = a.A; for (size_t j = 0; j < 10; ++j) std::cout << ' ' << A[j]; std::cout << std::endl; }
this is not going to work - you need to define a copy constructor,
otherwise you still passing an address fo the first element i.e. not
copied first element. please correct me if I am wrong!
puzzlecracker wrote: The only way to pass an array by value is to encapsulate it in a struct: #include <iostream>
struct Array { int A[10]; };
void f(Array a) { int* A = a.A; for (size_t j = 0; j < 10; ++j) std::cout << ' ' << A[j]; std::cout << std::endl; }
this is not going to work - you need to define a copy constructor, otherwise you still passing an address fo the first element i.e. not copied first element. please correct me if I am wrong!
#include <iostream>
struct Array {
int A[10];
Arrray(const Array &a)
{
if(a)
while(*A++=*a++ );
else
A=0;
}
};
void f(Array a) {
int* A = a.A;
for (size_t j = 0; j < 10; ++j)
std::cout << ' ' << A[j];
std::cout<<std: :endl;
puzzlecracker wrote: The only way to pass an array by value is to encapsulate it in a struct:
#include <iostream>
struct Array { int A[10]; };
void f(Array a) { int* A = a.A; for (size_t j = 0; j < 10; ++j) std::cout << ' ' << A[j]; std::cout << std::endl; }
this is not going to work - you need to define a copy constructor, otherwise you still passing an address fo the first element i.e. not copied first element. Please correct me if I am wrong!
You are wrong.
You can verify this by writing a simple test program:
cat main.cpp
#include <iostream>
struct Array {
int A[10];
};
void f(Array a) {
int* A = a.A;
for (size_t j = 0; j < 10; ++j)
std::cout << ' ' << A[j];
std::cout << std::endl;
}
int main(int argc, char* argv[]) {
Array a;
int* A = a.A;
for (size_t j = 0; j < 10; ++j)
A[j] = j;
f(a);
return 0;
}
then compiling and running it.
"Berk Birand" <gr******@yahoo .com> wrote in message Thanks to both for your answers. Also another question that pops in my mind is whether to use references or pointers in function headers. Is it better practice to declare a function as, say:
void afunc(int& i);
or is it better to declare it as
void afunc(int* i);
If one is allowed to pass in a NULL integer (not zero, which is something),
meaning that 'i' is not supplied, then you have to choose the second
version. Otherwise prefer the first method as it makes it clear that the
caller has to pass in a valid integer.
I know that in the second case, one has to explicitly dereference i whenever one wants to access it. But this can be considered a good thing, as it makes it more obvious that you are manipulating pointers, and the values are going to change. On the other hand, it's just more
work!
It's more work to type, not to the end program usually. Most compilers
implement references as pointers, except the pointer notation is not visible
to the end user. So both ways are on an equal footing, and which way is
better is your choice. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Seeker |
last post by:
Newbie question here...
I have a form with some radio buttons. To verify that at least one of the
buttons was chosen I use the following code ("f" is my form object) :
var btnChosen;
for (count = 0; count <= 1; count++) {
if (eval(f.RadioButtons.checked)) {
btnChosen = true;
}
|
by: Kenny |
last post by:
Hello,
can anyone tell me how to pass an array to a function ?
I have this function , part of my class.
It works if I do not put in int a everywhere , but obviously , I need to
add an array so I can keep everything neat and tidy, And to call the array
whenever I want
thanks
kenny
|
by: Mark Dicken |
last post by:
Hi All
I have found the following Microsoft Technet 'Q'
Article :-
Q210368 -ACC2000: How to Pass an Array as an Argument to a Procedure
(I've also copied and pasted the whole contents into the bottom of
this email)
|
by: wilson |
last post by:
Dear all,
In this time, I want to pass array to function.
What should I declare the parameter in the function?i
int array or int array? Which one is correct?
/********************************************************
Below is my code:
********************************************************/
|
by: nospam |
last post by:
Hello!
I can pass a "pointer to a double" to a function that accepts
double*, like this:
int func(double* var) {
*var=1.0;
...
}
| |
by: Alan Silver |
last post by:
Hello,
I'm a bit surprised at the amount of boilerplate code required to do
standard data access in .NET and was looking for a way to improve
matters. In Classic ASP, I used to have a common function that was
included in all pages that took an SQL query and returned a disconnected
recordset. This meant that data access could be achieved in a single
line. I would like to do something similar in ASP.NET.
I know I could just duplicate...
|
by: Abhi |
last post by:
I wrote a function foo(int arr) and its prototype
is declared as foo(int arr); I modify the values of the array in the
function and the values are getting modified in the main array which
is passed also. I understand that this way of passing the array is by
value and if the prototype is declared as foo(int *), it is by
reference in which case the value if modified in the function will get
reflected in the main function as well. I dont...
|
by: QQ |
last post by:
I have one integer array
int A;
I need to pass this array into a function and evaluate this array in
this function
how should I pass?
Is it fine?
void test(int *a)
|
by: IRC |
last post by:
hey, i am pretty new on javascript as well as PHP,
Hey, anyone can you help me, how to pass the javascript array value to
php page.........
i want to retrieve the values which are arrayed on "selectedValues"
from next page for php variable
this is my javascript code saved on "sendValue.js" file,
<script>
function updateRecordEntry(requestValue){
|
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 ways of passing an array. In the following code, fun1(int
a1) - same as fun1(int* a1) - where both are of the type passed by
reference. Inside this function, another pointer a1 is created whose
address &a1 is different from that of the passed...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |