Accessing struct variables outside of function in C -
Accessing struct variables outside of function in C -
i having problem understanding scope of variables within struct. 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 variable *name set "thomas" within void setname()? how create if give value struct variable that value accessible globally. if print out variable name within int main() blank, how create print out "thomas"? or doable within function setname()?
is variable name
set "thomas"
within void setname()
?
yes. that's essence of how local variables work
how create if give value struct variable that value accessible globally?
make struct class setname;
global declaring outside setname
function. suggest giving different name - say, theclass
.
you should alter setname
take both name , struct class
on set name.
if print out variable name within int main()
blank, how create print out "thomas"
?
once move declaration outside setname()
function, able access 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); homecoming 0; }
c variables pointers struct
Comments
Post a Comment