What is the purpose of the struct keyword?
What is the purpose of the struct keyword?
to define structs... what else?
and to maintain backwards compatability with C.
good one YGF...
struct defines a conglomerate user data type. for example:
etc...Code:struct my_struct { int element_a; char element_b; }; my_struct struct_instance; struct_instance.element_a = 0xffff; struct_instance.element_b = '!';
hasafraggin shizigishin oppashigger...
I've found that struct is most easily understood when put into this context.
We know that an array can hold a large collection of data. The problem with array's is that all elements in an array be it multi dimensional or single dimension, must be of the same data type.
With a struct, you define a brand new data type made up of a bunch of variables.
Let say you wanted to maintain an array of 500 students in a school. Their first and last names and their grade point averages and their ages. It might look like this.
Now I have a single array containing multiple data types for each element in the array and all items in that array element pertain to a single student.Code:struct students { char *FirstName; char *LastName; int age; double grade; }; // define an array of my new students data type Students MyStudents[500];
If you know VB at all and are familier with the TYPE keyword in VB it would look like this;
The struct block can also contain functions written inside the struct just like you would do with a class. From what I've read, structs were the basis from which classes where born and I can see why that might be but whose to say for sure if that's true.Code:Publis type students FirstName as String LastName as String Age as Integer Grade as Double End type ' Now lets define our vairable to use our new User Defined Type or UDT. Dim MyStudents(500) as Students
I hope this has been somewhat helpful. I'm just learning C++ myself and struggling with my own challenges..
Twin engine aircraft are way better. If one engine quits the other takes you to the scene of the crash.
use structs to pass several pieces of data to a function with one parameter
PHP Code:
//mytriangle.h, structures don't compile, but define a new data type and tell the
//compiler what this data type is made of, so it goes in a header file
struct XYpair//XYpair is the data type
{
double X;
double Y;
};
struct triangle//triangle is a new data type
{
XYpair point[3];//3 XYpair data types in the triangle data type
};
double calculateArea(triangle nameOfParam);
//triangleExampleMain.cpp
int main()
{
triangle NameOfTriangle;//declare a peice of data of type triangle
//now you must define your new triangle, some people call it "filling in" the structure
NameOfTriangle.point[0].X=2;
NameOfTriangle.point[0].Y=56.8;
NameOfTriangle.point[1].X=4.32;
NameOfTriangle.point[1].Y=4.78;
NameOfTriangle.point[2].X=3.5;
NameOfTriangle.point[2].Y=7.6;
double AreaOfTriangle;//declare this to store some data
//assume we have this function implemented somewhere already
AreaOfTriangle = calculateArea(NameOfTriangle);
//I just passed calculateArea 6 double values with one parameter
//you could a 2D array to represent points like point[3][2] but it's not as readable, or you could define the points individually and
// run the risk of passing points from two different triangles to one function, that would be a mess
return 0;
}
WebSnozz-
Cats have no butt cheeks.
If one farted, then it would make a flute noise.