Originally Posted by
Vipul Motiwala
Hello Everybody... I'm a student and i have recently started learning C++.
We are learning functions right now.. somebody please tell me how to use arrays given in the
main function in some other user defined function...
like in the prog:
void main()
{
void array();
int n[*0]={*,2,*,4,5,6,7,8,*,*0}
getch();
array()
}
void array()
{
int var[*0];
cout<<var;
}
u see i want to use the value given in the main function i.e. value stored in array n in my function 'array'... how can i do that.. plz tell... if an example can be given i'll be thankful
There are many ways, but here are a few:
Code:
void array(int var[*0]);
int main()
{
int n[*0] = {*,2,*,4,5,6,7,8,*,*0}
getch();
array(n)
}
void array(int var[*0]);
{
cout << var;
}
Code:
void array();
int n[*0] = {*,2,*,4,5,6,7,8,*,*0}
int main()
{
int n[*0] = {*,2,*,4,5,6,7,8,*,*0}
getch();
array(n)
}
void array();
{
cout << n;
}
There's probably mistakes in there, but you get the idea. You can pass the array in through the function parameters, or you can declare the data globally.
One problem with your code was that you declared the array function inside main(), but it should be declared before any of that. I fixed it in both those pieces of code.