What is the purpose of the struct keyword?

This is a discussion on What is the purpose of the struct keyword? within the C++ Programming forums, part of the General Programming Boards category; What is the purpose of the struct keyword?...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    30

    What is the purpose of the struct keyword?

    What is the purpose of the struct keyword?

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    to define structs... what else?


    and to maintain backwards compatability with C.

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    good one YGF...

    struct defines a conglomerate user data type. for example:

    Code:
    struct my_struct
    {
      int element_a;
      char element_b;
    };
    
    my_struct struct_instance;
    
    struct_instance.element_a = 0xffff;
    struct_instance.element_b = '!';
    etc...
    hasafraggin shizigishin oppashigger...

  4. #4
    Assembler + Basic from 79
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    22
    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.

    Code:
    struct students {
       char *FirstName;
       char *LastName;
       int age;
       double grade;
    };
    
    // define an array of my new students data type
    Students MyStudents[500];
    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.

    If you know VB at all and are familier with the TYPE keyword in VB it would look like this;

    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
    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.

    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.

  5. #5
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-03-2008, 12:10 AM
  4. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 05:07 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21