legal or illegal


Is this legal according to C++ or illegal?
Notice the location of int main, and the arrays specifically.

const int BOOKS = 21; // 8 Parallel arrays, delclared globally
char isbn[BOOKS]; // Parallel Array 1
char author[BOOKS]; // Parallel Array 2
char publisher[BOOKS]; // Parallel Array 3
char dateAdded[BOOKS]; // Parallel Array 4
int quantity[BOOKS]; // Parallel Array 5
double wholesale[BOOKS]; // Parallel Array 6
double retail [BOOKS]; // Parallel Array 7
char bookTitle[BOOKS]; // Parallel Array 8
//******************************************************************************************
// This function returns us to the main menu *
//******************************************************************************************
int main()
{
Thats legal. It's declaring them in file-scope. Almost global.

Bad practice though :)
Thanks Zaita, but what do you mean almost global?

How do I get to be fully global?
I think he's wrong...I think they are fully global.

Not sure though, haven't declared a global variable in while though...usually just use a pointer for that.
closed account (z05DSL3A)
This is one of those times where there is a little confusion. A name that is not declare in a block, class or namespace has file scope. So it can be used anywhere in the file after the point that the name was declared. A name, with file scope, used for a variable, object or function (not declared using the static keyword) is called global. However, in the above example, the declarations would appear to be in the mains .cpp file that would restrict them to being used only within this file. To break out of the file scope you would have to use the extern keyword in the declaration.

HTH
// I actually have them set up like this, notice int main()'s location
// and the fact that the arrays are not within int main(), but outside of int
// main(). Furthermore, I have noted a lot of people pointing out a very
// valid argument that it is bad practice to have this type of set up; however,
// I am just starting to learn about C++, and would like to experience what
// not to do and what to do the right way, so I will probably change the
// location of the arrays to within int main(), instead of out of int main.
// Again thank you for everyone that responded.



const int SIZE = 20;
string isbn[SIZE];
double price [SIZE];
string bookTitle[SIZE];

int main()
{
Last edited on
Any variables that are declared outside of main are global, in that other files may use them, if they specified with the extern keyword. Variables that are declared outside of any function with the static keywork are of file scope because they can never be legally used by other files. Any attempt by other files to use them via the extern keywork will cause a linking error. Grey was technically right in that other files that attempt to use them must use the extern key, that doesnt mean that the keywork 'breaks' those variables declared outside of any function WITHOUT the static keywork out of file scope.

In order to really understand this you have to understand how code is compiled and linked into machine code. First all source files are compiled seperately. So in order for the compiler to know what to do variables and functions in other files must be prototyped using the extern keyword. That way when they are used the compiler knows that they are in another file and insert a placeholder. That placeholder specifies to the linker that that reference should be replaced by the one in the other file(s). So that is a very quick description of the C++ build process.

Also in regards to global variables, they can be useful if you know how to use them. In embedded applications I have found that they are sometimes they are the only way to synchronize execution in different files of the application
Topic archived. No new replies allowed.