-2

Why should structures be defined outside the main function for other functions to be able to receive them? I understood this answer, but why is it possible for normal variables declared in the main function (e.g. int and float) to be used as arguments for a function but not structures defined in main?

(PS: I asked this question because I still don't have enough reputation to comment!)

Community
  • 1
  • 1
Leponzo
  • 624
  • 1
  • 8
  • 20
  • When you say "a structure", are you talking about a type (defined with `struct thingy {...stuff...};`), or are you talking about variables with that type (defined with `struct thingy myThingy;`?) – user253751 Dec 22 '15 at 02:58
  • I'm referring to functions that receive instances of a structure - i.e. variables with that type. – Leponzo Dec 22 '15 at 03:00
  • so "why can't you pass variables defined in `main` to other functions if they have structure type?"? The answer to that is you can! – user253751 Dec 22 '15 at 03:02
  • Alternatively, if you're thinking of the *type* being declared inside `main`: The reason for the difference is essentially that `int` and `float` aren't declared inside a function. (Just like struct types declared outside any function) – user253751 Dec 22 '15 at 03:03

2 Answers2

2

One word: "Scope".

If your struct's type is defined is in main() then the type's scope is main. Nothing outside main can know about it. If you want to pass it to another function, obviously that function has to know the type, but if the type is only known in main then obviously the function can't know about it.

The solution? Move the type's scope to somewhere that both main and the "receiving" function can see it.

Edit: built in types like int, char etc already have global scope and so they are known everywhere. What we're dealing with here is a user define type whose scope is limited based on where it is defined.

The same rules apply to variables (aka instances of a type) - a global variable is visible anywhere (I know, you can "hide it" with a local of the same name - lets not split hairs eh ;-), but a local variable in main() can only be seen inside main().

John3136
  • 28,809
  • 4
  • 51
  • 69
  • Yes, but I wanted to know why this works for normal variables defined in main (e.g. int x). Shouldn't their scope also be restricted to main only? – Leponzo Dec 22 '15 at 03:02
  • The reason it works is that other types are _intrinsic_, such as, `char, short, int, long, double, ...` so everybody knows about them – Craig Estey Dec 22 '15 at 03:03
  • 3
    @Leponzo You're confusing the *type's* scope with the *variable's* scope. – user253751 Dec 22 '15 at 03:04
  • 1
    @Leponzo And it seems you are also mixing up the concept of *variables* and *types*. A base type like `int` is available everywhere. But a variable declared in `main` (or any function) is only available in `main` - regardless of whether the variable is of a base type or a struct. – kaylum Dec 22 '15 at 03:06
0

You need to understand the concepts of scope and primitive data types. Structures are not primitive types but user defined constructs, so if you define a structure in a given scope it will not be known to the program outside of that scope, because it was you who defined it so it is restricted to the scope where you did define it. Primitive data types like (int, float, char) defined in the language and are known to the whole program. If they were not available everywhere, it would actually be impossible to define main() itself, since

int main(void)
{
    return 0;
}

This simple definition of main() required int to be "defined" already.

Finally, to be able to define structures you also need these primitive types because you define a structure as an object that contains one or more of these primitives. You can of course use previously defined structures as members to structures and so on.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97