Friday, March 13, 2009

Structures

I just learned about structures yesterday. I'm going to write up what I learned to reinforce some knowledge.

struct Person {char Name[20]; int Age; float Height;};

That's how you declare a structure. According to Prata, you want to externally declare structures so all functions can make use of them. Yes, programmers discourage declaring global variables or externally declaring variables, but recommend the external declaration of structures. This is how you use structures in the main function after having defined the structure before the main:

Person you = {
"CppNoob",
20,
172.0
};

or Person you = {"CppNoob", 20, 172.0}; // all the same

Now you can print you's values

cout<<"Name: " << you.name << " Age: " << you.Age << " Height:" << you.Height;

No comments: