Function which returns an array




















Hence it is clear from the output, that the array return by the function func was successful. Your email address will not be published. Leave a Reply Cancel reply Your email address will not be published. Leave this field empty. Exact matches only. Search in title. Because of this, you are now on your own to ensure that you are not going to overrun the array.

We can also return the array. To return the array , the return type of the function should be of structure type ie marks. This is because in reality we are passing the structure that contains the array. So the final code may look like this. As above mentioned paths are correct.

But i think if we just return a local array variable of a function sometimes it returns garbage values as its elements. Which is something like this. However, you can return a pointer to an array by specifying the array's name without an index. Actually when you pass an array inside a function, the pointer to the original array is passed in the function parameter and thus the changes made to the array inside that function is actually made on the original array.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Return array in a function Ask Question. Asked 11 years, 3 months ago. Active 1 year, 2 months ago. Viewed k times. How will I use it, say I returned a pointer how am I going to access it? Improve this question.

Ismail Marmoush Ismail Marmoush As long as you're not making the mistake of creating an array on the stack and returning a pointer to it. BuggerMe: Arrays are not passed by reference unless you request it with a much funnier syntax , in the code, the array decays into a pointer to the first element and that is passed to the function.

The 5 in the function signature is discarded by the compiler. BuggerMe: Not, not really. When you pass by reference what you get inside the function is a reference to the actual type. Calling foo myarray produces the decay of the array to a pointer to the first element. Show 5 more comments. Active Oldest Votes. This syntax that you're using: int fillarr int arr[] Is kind of just syntactic sugar. Improve this answer. GManNickG k 50 50 gold badges silver badges bronze badges. Brent Nash, no.

A pointer to the start of the array is a pointer. It just happens that the compiler has some syntactic sugar that does the translation for you in some situations. Brent: No. An array is it's own type, it's not a special kind of pointer. The type of a in int a[10] is int[10]. What you can say is arrays "decay" into pointers to their first element. This is an implicit array-to-pointer conversion. Then your answer would go along the lines mine does. If you edit your answer to differentiate between arrays, array-to-pointer conversion, and pointers, I'll delete my answer since they would have the same core information and you were first.

Show 13 more comments. Nawaz k gold badges silver badges bronze badges. Potatoswatter Potatoswatter k 22 22 gold badges silver badges bronze badges. But you are wrong in that you cannot return an array by reference. Simple, isn't it? David: thanks, I got the misimpression from the Comeau message error: function returning array is not allowed which occurs if you leave out the outer parens in the non-typedef syntax.

Fortunately, today I reviewed the right-left rule for another question and managed to construct the right thing… after seeing you say it's possible… before seeing that you gave the code :vP. The answer by chubsdad has the correct quote from the standard: you cannot return an array, but you can return a reference or pointer to an array.

Arrays are non-copyable as a type and as such they cannot be returned --which would imply a copy-- and when that syntax is present the compiler will convert the argument into a pointer. David: So it does. This page is getting to be bizarrely long. Never have so many people voluntarily written so many trivial functions returning an array in one place.

Potatoswatter I am new to cpp, Can you explain the 2nd code snippet in detail? I am not able to break it into parts for the sake of understanding. Show 4 more comments.

Python Turtle. Verbal Ability. Interview Questions. Company Questions. Artificial Intelligence. Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics. Ethical Hacking. Computer Graphics. Software Engineering. Web Technology.



0コメント

  • 1000 / 1000