- 16,335
- 258
mathmari said:Should I declare [m]starsystem[/m] outside the if?? (Wondering)
The would help yes.
The life time of an object ends when we get to the matching closing brace. (Nerd)
mathmari said:Should I declare [m]starsystem[/m] outside the if?? (Wondering)
I like Serena said:The life time of an object ends when we get to the matching closing brace. (Nerd)
mathmari said:What do you mean?? (Wondering)
void function()
{
int a = 2;
{
int a = 3;
printf("%d\n", a);
}
printf("%d\n", a);
}
I like Serena said:When you have:
what do you think gets printed? (Wondering)Code:void function() { int a = 2; { int a = 3; printf("%d\n", a); } printf("%d\n", a); }
mathmari said:It will print:
3
2
right?? (Wondering)
I like Serena said:Yep. (Nod)
When "int a = 3" is declared, local stack memory is allocated for it.
Furthermore it "hides" the "a" that was declared earlier.
When the closing brace comes by, the local stack memory is automatically released.
Afterwards, the original "a" becomes visible again. (Nerd)
mathmari said:Ahaa... Ok! (Muscle)I have also an other question...
When I declare 'starsystem', before the if statement, do I write it as followed??
[m] starsy_t *starsystem;[/m]
Or should I set it equal to [m]NULL[/m] ??
(Wondering)