2

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()?

7
  • The struct is the same as any other local variable. You can't access local variables to a function from outside the function. Commented Oct 29, 2014 at 19:35
  • You will have to malloc the memory for "thomas" if you want your global struct to still point to it. I suggest returning the char* from the function in order to be able to free it later. Commented Oct 29, 2014 at 19:35
  • If you tried to print it out within main(), you'd get a compiler error, since the setName variable is only declared within the setName() function. It's not that setName.name is "unset" when the function exits, it's that the setName struct doesn't exist outside the function in which it's declared. Commented Oct 29, 2014 at 19:36
  • @LeatherFace What "global struct" do you see in the example? Commented Oct 29, 2014 at 19:36
  • He will have to declare a global structure then delete the local structure in the function. Commented Oct 29, 2014 at 19:37

1 Answer 1

4

Is the variable name only set to "thomas" within void setName()?

Yes. That's the essence of how local variables work

How would I make it so that if I give a value to a struct variable that that value is accessible globally?

Make struct Class setName; global by declaring it outside setName function. I suggest also giving it a different name - say, theClass.

You should also change setName to take both the name and struct Class on which you would like to set the name.

If I was to print out the variable name within int main() it would be blank, how would I make it print out "thomas"?

Once you move the declaration outside setName() function, you would be able to access it from anywhere you'd like.

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;
} theClass;

void setName(struct Class *myClass, const char* theName) {
    myClass->name = theName;
}

int main() {
    setName(&theClass, "thomas");
    printf("%s\n", theClass.name);
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, perfect explanation. 10/10.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.