Stylistically is it possible to declare structs on top followed by their definition after int main? I tried doing it the same way as one would declare functions before main but it doesn't seem to work.
-
"on top" and "after int main()", don't they yield opposite meaning?– sinisterCommented Dec 5, 2014 at 2:46
-
For functions we can declare a prototype first. But we can define the function at the end of the code, after int main. Can we do this with structs as well?– LucasCommented Dec 5, 2014 at 2:49
-
sorry, my mistake.– sinisterCommented Dec 5, 2014 at 2:54
-
I have to ask...why? Even if it's possible it seems like a poor style choice– lethaljdCommented Dec 5, 2014 at 2:58
-
Ok, yes, it does seem confusing to read. I guess I'll stick with the convention. Thanks for your input.– LucasCommented Dec 5, 2014 at 3:07
2 Answers
We do that for functions in order for the compiler to know that there's a function with this signature that exists in our source code so that it doesn't get to a function call before it's defined and say "OK. I don't know what that is!".
For structs, on the other hand, if you're trying to declare a variable of a struct type before it's defined, that'll be an error since the size of the struct will be unknown at this point.
However, you can still declare structs and define them separately and that's how you do it
// declaration
struct s;
// definition
struct s
{
// members
};
I'm not sure if this is particularly useful in a way or another.
-
ah.. Now I see what @Lucas was asking for... I misread his question (sorry about that, @Lucas!). @Kareem: is it posible that your example code, as it is, would result in error? As per " if you're trying to declare a variable of a struct type before it's defined, that'll be an error since the size of the struct will be unknown at this point." Shouldn't it be done exactly the other way around? (I mean, first
// definition
and only then// declaration
). (No "tricky question" here, just not totally sure =P)– abelinuxCommented Dec 5, 2014 at 16:57 -
@abelinux I never declared a variable of a struct type before the struct's definition. I declared the struct itself.– kzidaneCommented Dec 5, 2014 at 17:04
-
-
Thanks for helping out abelinux and kareem! Yes, that's what i tried to do except I added typedef in front of the struct.– LucasCommented Dec 6, 2014 at 0:19
You could, actually, and it should work. Try this out:
#include <stdio.h>
struct date
{
int day;
int month;
int year;
};
int main(void)
{
struct date MyBirthday;
MyBirthday.day = 36;
MyBirthday.month = 15;
MyBirthday.year = 2165;
printf("%s: %d/%d/%d\n", "My birth date was",
MyBirthday.day, MyBirthday.month, MyBirthday.year);
return 0;
}
It doesn't make much of a difference, though, declaring global variables outside main()
if you're calling every other function in your program from within main frame: every single variable declared within main()
will also be available to any other function called from within it. Which, in practice, is what global variables do ;)
What could be useful is declaring certain predefined structs that could be used from within several files / programs.
E.g.:
struct date
{
int day;
int month;
int year;
};
seems like a struct you could use across several different programs.
So you can declare it in a header file, which in turn can be included in different software as needed.
Is this what you where asking?
HTH!