English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

ALL arrays in C++ are passed as reference??

CAN ANY BODY TELL ME THAT IS IT TRUE??

2007-04-17 21:06:08 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

yes, but you can pass individual elements within the array by value.

example:

char myarray[10] = "hello";
passbyvalue(myarray[0]);
//passes the letter "h" to the function

2007-04-18 00:28:14 · answer #1 · answered by justme 7 · 0 0

Yes. Arrays are always passed by Reference.

2007-04-17 21:14:35 · answer #2 · answered by Sceptic 2 · 0 1

Yes arrays are passed by name

2007-04-18 03:06:51 · answer #3 · answered by Anonymous · 0 0

nicely, while a function is spoke of as, the parameters are placed on the call stack. that's so as that the values would be examine off the stack via the function this is spoke of as. in case you bypass via fee, that would desire to require that the completed array and the different parameters are all pushed onto the stack in earlier the function would be spoke of as. and additionally back while the function returns. That eats up efficient CPU cycles, and memory (the stack). the two certainly one of that are no longer reliable to do, enormously while you're engaged on a software that ought to be speedy and/or multithreaded. once you bypass the array via reference, it in basic terms pushes the handle of the parameter onto the call stack (and the different parameters as needed), and then calls the function. because of the fact the scale of an handle of a variable in memory is often the same, the function call wastes much less time in simple terms getting the values, and extra time on the needed processing of those values. The "const" is utilized in C/C++ to define parameters/applications that are no longer allowed to get replaced in the function. as an occasion, in case you had this code, and it took a million CPU cycle to push each and each fee onto the stack:    int myArray[5000];    void mySlowFunctionCall(int anArray[]);    void myFastFunctionCall(const int &anArray, unsigned length); mySlowFunctionCall(myArray); - might take 5,000 CPU cycles earlier calling the function, and yet another 5,000 CPU cycles earlier getting back from the function, yet myFastFunctionCall(myArray, 5000); - might take basically 2 CPU cycles earlier calling the function, and yet another 2 CPU cycles earlier getting back from the function Now, think of in case you surpassed 2 arrays into the "mySlowFunctionCall()", and "myFastFunctionCall()". The "mySlowFunctionCall()" might then take 10,000 CPU cycles earlier and 10,000 CPU cycles after calling the function, yet "myFastFunctionCall() might basically take 4 CPU cycles earlier and four CPU cycles after calling the function. with any luck this clears it up for you.

2016-11-25 19:10:15 · answer #4 · answered by vallee 4 · 0 0

fedest.com, questions and answers