Greetings hopeolicious,
A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
Structure Basics
The keyword
struct introduces a sturcture declaration, which is a list of declarations enclosed in braces.
struct MyBox {
int left;
int right;
int top;
int bottom;
};
An optional name called a structure tag may follow the word
struct. The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces.
A struct declaration defines a type. The right brace that terminates the list of members may be followed by a list of variables, just as for any basic type.
A structure declaration that is not followed by a list of variables reserves no storage; it merely describes a template or the shape of a structure. If the declaration is tagged, however, the tag can be used later in definitions of instances of the structure. For example, from the given declaration above:
This defines a variable box_1 which is a structure of the type
struct MyBox. A structure can be initialized by following its definition with a list of initializers, each a constant expression for the members:
struct MyBox box_1 = {0, 5, 0, 10}; An automatic structure may also be intialized by assignment or by calling a function that returns a structure of the right type.
The structure member operator "." connects the structure name and the member name. To print the coordinates of the integer "left" of box_1 for instance would compute as the following:
The syntax is simple,
structure-name . member
Implementing
As continuing from above, you may see that your structure contains a few syntactial errors:
The red-colored section(s) may cause an error, and the green-colored section(s) show what needs to be added. Before I move to member-calling, let me clarify a few things. Your struct called
hope cannot be called on if your structure tag is the same. You will have to either name your structure differently while defining the variable of your struct to hope:
struct Hope {
...
};
[/b]Hope hope; Or just choose a different name while declaring the variable name declaration.
On to the member calls, when you call on something, say, f_acost, you cannot call it normally. You will have to take the neccessary steps to call on such a structure member, for example purposes:
You will have to find all members from within your structure and change all to look like the above.
Here is an example of how to allow all of your functions to see and recognize your structure:
struct Hope {
float f_acost;
};
Hope hope;
int main() {
hope.f_acost = 0.0f;
return 0;
}
Conclusion
I hope this helps. If you have further questions, please feel free to ask.
-
Stack
Overflow