Give a program code illustrating the difference between passing an array by reference and by address..
How to pass an array(by reference and by address)in C/C++?
1. In C you pass arrays by reference by default. You pass everything else by value by default.
Passing by reference and passing by address are the same thing. In C, to explicitly pass by reference, when it isn't an array you declare it with:
int Myfunc(int *MyVar);
then in the main() function you call it with:
Success=Myfunc(%26amp;MyVar);
In other words, Myfunc takes a pointer to or address of MyVar (*MyVar) and you actually send it the address (%26amp;) when you call it.
With C++ all you need is that address operator. You declare:
int Myfunc(int %26amp;MyVar);
then in main() you call it with:
Success=Myfunc(MyVar);
But arrays are always passed by references, and that means passed by addresses: They are the same thing.
I'm not citing Kernighan and Ritchie's the C programming language in this answer, our Stroustrup's the C++ programming language but if you don't have them GET THEM.
Reply:Don't listen to Rowell A, Visual Basic is a crap language compared to C++, but enough of the jibber jabber about which language is best.
Array's in C++ are pretty easy to understand once you get the basics down.
For a full explanation of how to pass an array to a function via reference, go to this website: http://www.cplusplus.com/doc/tutorial/ar...
Once you are on that site, scroll down to the section that reads: "Arrays as parameters".
In basis, passing an array to a function is practically the same as passing a variable to a function, but you add the elements of the array (the number inside the brackets ([ ])).
EDIT__________________________________
Very well said jplatt39, i gave you a thumbs up :) I haven't fully gotten up to pointers in my self education of c++, I just got up to building basic structs and dealing with vectors. I haven't gotten passing and whatnot down completely (pointers haven't been learned yet). I will learn them soon though :)
Reply:gudluck with that one. array in c++ that so hard. it's easier on visual basic and other languages but c++ is so old.
floral deliveries
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment