Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to make a array globle to 3 different functions

Status
Not open for further replies.

158

Programmer
Nov 15, 2003
5
US
Not sure how this is done, this is what I know. Declare array in main, but not fimilar with how to write the argument in the function to call the array. Also not sure if I need to put the "&" in the function argument so I can change the date of the array or does it not need it and allow editing.

Hope I got my point across
Any help with this would be great
Been working on this program for a few hours now mainly this part and can't figure it out

Thanks.
 
Here is my sloppy code that I have been working on its not complete but gives you a idea of what i'm trying to do.

#include <iostream.h>
#include <iomanip.h>

char fillArray();
char fowardArray();
char backArray();

main ()

{

cout << setprecision (2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);


fillArray();
fowardArray();
backArray();


cout << &quot;\n\n&quot;;

return 0;

}

//*******************************************
char fillArray()
{

int i;
char MyWord = 'A';
char MainArray[16];

for (i=0; i<=15; i++)

{
MainArray = MyWord;
MyWord++;
}

return MyWord;
}
//*******************************************




//*******************************************
char fowardArray()
{

cout << &quot;\n\n&quot;;

int m;
char MyWord = 'A';
char MainArray[16];

for (m=0, m<=15, m++)
{
MainArray[m] = MyWord;
MyWord++;
cout << MainArray[m];
}

return MyWord;

}
//*******************************************




//*******************************************
char backArray()
{

cout << &quot;\n\n&quot;;

int n;
char MyWord = 'P';
char MainArray[16];

for (n=15, n>=0, n--)
{
MainArray[n] = MyWord;
MyWord--;
cout << MainArray[n];
}

return MyWord;

}
//*******************************************
 
put the array declaration outside of any functions blocks to make it global to all.
 
How does a function call the global array?
 
making it global is the easiest way to do it, but if you want to explicitly pass it to individual functions then you have to declare it in the function parameters.

// Prototype
char fillArray(char []);


// Function definition

char backArray(char MainArray[])
{

cout << &quot;\n\n&quot;;

int n;
char MyWord = 'P';
// char MainArray[16]; <--- get rid of this

for (n=15, n>=0, n--)
{
MainArray[n] = MyWord;
MyWord--;
cout << MainArray[n];
}

return MyWord;

}




// Calling the array

void main()
{
char MainArray[16];

backArray(MainArray); // don't include the [] here

return;
}





Arrays are always passed by reference so you don't need to include the &quot;&&quot;.
 
a global variable is a variable that can be seen by all the functions.

You can just call it normally from any function like it was a local variable.

Provided you're calling it from a function that's written in the same file that your declared the variable in. You can even make it global to multiple files if you add &quot;extern&quot; in front of it.

 
Very helpful going to start working on this
Thank you
 
Just finished it works perfect, you were a big help thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top