I am having trouble understanding the scope of variables within a struct. For example:
struct Class
{
const char *name;
int Hitdice, Str_Dice, Dex_Dice, Con_Dice, Int_dice, Wis_Dice, Cha_Dice, Skill_Points, level;
double BAB_Type;
struct Class *next_Class;
};
void setName()
{
struct Class setName;
setName.name = "thomas";
}
int main()
{
}
Is the variable *name only set to "thomas" within void setName()? How would I make it so that if I give a value to a struct variable that that value is accessible globally. If I was to print out the variable name within int main() it would be blank, how would I make it print out "thomas"? Or is that only doable within the function setName()?
structis the same as any other local variable. You can't access local variables to a function from outside the function.main(), you'd get a compiler error, since thesetNamevariable is only declared within thesetName()function. It's not thatsetName.nameis "unset" when the function exits, it's that thesetNamestruct doesn't exist outside the function in which it's declared.