Does that work? I would like to run some code in the main, then it jumps to a void, where an array is given its ints, then it should return to the int main () where it continues using the array with the ints from the void.
There are two possibilities: either pass an array by reference or pass an array by value. When an array is passed by value it is converted to the pointer to its first element. I will show two variants.
#innclude <iostream>
void fill( int *a, int n )
{
for ( int i = 0; i < n; i++ ) a[i] = i;
}
constint N = 10;
void reverse_fill( int ( &a )[N] )
{
for ( int i = 0; i < N; i++ ) a[i] = N - i - 1;
}
int main()
{
int a[N];
fill( a, N );
for ( int i = 0; i < N; i++ ) std::cout << a[i] << ' ';
std::cout << std::endl;
reverse_fill( a );
for ( int i = 0; i < N; i++ ) std::cout << a[i] << ' ';
std::cout << std::endl;
return 0;
}
The difference between the functions is that in the second function that gets the array by reference the size of the array shall be a constant expression. It is why constant N was declared before the function declaration.
Wouldn't it be better to say "array pointer" otherwise to me it sounds like a copy of the array is being made and any modifications won't effect the original.
@xerzi
Wouldn't it be better to say "array pointer" otherwise to me it sounds like a copy of the array is being made and any modifications won't effect the original.
You are not specifying a pointer as the argument. You are specifying the array which implicitly will be converted to the pointer to its first element.
And I do not know such term as "array pointer". Atl least there is no such a term in the C++ standard.
I do not know also what is "subset of C++". I know C++ and try to use terms that are defined in the C++ standard. I do not bother that somebody who does not live in "the subset of C++" can be confused . Every programming language has its own system of terminolodgy that every one should follow. Moreover this is a forum on C/C++.
And as I menitioned already above there is no such term as "array pointer" in the C++ standard. So it is such a term as "array pointer" that can confuse everybody who knows C++.
"pass array by value" is not C++ terminology, it is common amongst all programming languages and describes how an object is to be passed to a function. The only reason this would be meaningful is if you know specifically how c-arrays work, and even still when you say array I would be assuming std::array which would make your examples incorrect with your saying. Use proper terminology: "c-array".
So if you read in the standard there is a teapot orbiting the sun you would believe it, use some of your own brain power.
@xerzi
when you say array I would be assuming std::array
It is only your personal problem what you are assuming when somebody are speaking about arrays.:)
One more there is no such term as c-array in the C++ Standard. There is term array that denotes a compound type and std::array (I hope you see namespace name std) that denotes a standard template container.
Moreover this theme deals with the compound type array and nobody except you assumed that the author of the theme are speaking about the standard container std::array.
Moreover I'd like to point out that std::array appeared only in C++ 2011 so nobody was confused when somebody was saying about arrays. :)
As far as I can tell it's just me and you. So idk who you are talking about other then your imaginary peers.
If I were to say "string", which would you think I am talking about. Think about it, it is related. :)
As such again keep your mind open, the community makes their own distinctions to add clarification to things the standard does not. It is a community driven effort and if all you do is lock yourself in your room then continue.
appeared only in C++ 2011 so nobody (note: only you [vlad]) was confused
Then I suggest you read the new standard and get yourself up-to-date grandpa. :)
I know you are not a physician (unless you mean doctor of computer science, then sorry to hear that), I am trying to help you. You probably don't know what it means to communicate a thought, with clarity, to another person so they can understand you, as you are clearly using google translator with that terrible grammar.