Structures

Hi,
I just wanted to talk about structures. So I read somethings on line but I think its always good to talk. So I just wanted to know what the deal with this, what is the benefit. It seems like it is just a container to hold variables. So if you feel you have something to say on this I would like to hear the benefits.
What is so great about this ? Thanks
You know this?
1
2
3
4
5
6
7
struct product {
  int weight;
  float price;
} ;

product apple;
product banana, melon;

This topic would be better off in the Beginners or General programming boards. Anyway, structures were meant to give programmers a way to congregate different types of data that apply to a single object in one "type." Structures were a stepping stone to bigger and better forms of objects and so are lesser, but still important, components of OOP.
closed account (3qX21hU5)
To expand on what Raezzor said consider this example. Lets say you want a Object to hold the data for a student in your programming class. You could do something like this.

1
2
3
4
5
6
struct student
{
	string name;
	double midterm, final;
	vector<double> homework;
};


That way you can all the different variables in one "Object" even if they are different types. So you can think about the student's info as one Object instead of a bunch of different variables and types.
Raezzor wrote:
Structures were a stepping stone to bigger and better forms of objects and so are lesser, but still important, components of OOP.

In C, yes, but in C++, struct is equivalent to class, except that the default access is public instead of private.
That way you can all the different variables in one "Object" even if they are different types. So you can think about the student's info as one Object instead of a bunch of different variables and types.

So don't you just access it by a dot? So maybe (forgive the syntax) student.name? So is this just a way to conceptualize an object to make it easier to think about? Really it is just the conceptualization of an object? Instead of having all thease variables running around? I don't know if that makes sense I tried to explain what I was thinking.
Also if you have have more then one strudent is that a problem? Or can you do multiple strudent from the structure student? Just curious. I didn't post this in beginners because I did post a conceptual thing one time and they said "READ THIS" and gave me a link. I did read about structures but sometimes it is nicer to talk about them.
Thanks for the reply's

Oh and also my teacher hasn't talked about


1
2
3
4
struct movies_t {
  string title;
  int year;
} mine, yours;


I'm confused about the mine,yours;
We just use ; after our ; we haven't put anything before it like mine, yours.
Thanks
Last edited on
@chris Good point on the public/private. :) Wasn't sure if that was too much info or not, so I tried to make it as simple as possible.

@jlillie Yup, you access the data using the . operator just like you guessed. And as long as each object you declare has a different name you are fine. Basically, in your teachers example, each object (mine, yours) is of the TYPE of movie_t, just like int size is of TYPE int. All the teacher showed was a way to declare 2 instances of the structure. It would have been the same as this:

1
2
3
4
5
6
7
struct movies_t {
  string title;
  int year;
};

movies_t mine;
movies_t yours;
@jlillie
In addition to what everyone has said already, it can be hard to see a reason for using structs at all as you're learning about them. But as you get farther into it you eventually start to wonder how you could have worked without them.

An example:
I'm writing a server manager for a game I play called Terraria. Terraria stores information about 'worlds' the player plays in in binary text files. Part of my program is to pull this header information from the file, and the header contains upwards of 30 variables. I need to manage and control sometimes dozens of these headers. It would be impossible (well, not impossible, but extremely messy) to declare 30 variables every time i want to interface with a header. So I created a struct called WorldHeader that has all the variables I need. It's a lot about keeping things organized and easy to interface with.

@chrisname
That is a really good point that I was unaware of myself. Thanks for that.
closed account (3qX21hU5)
Think about the mine and your as variables of type movies_t.

And each movies_t variable has a sub category of string title, and int year.

So basically instead having to declare something like this to create 3 instances of my students struct with out using struct

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    string s1Name, s2Name, s3Name;
    double s1Midterm, s2Midterm, S3Midterm;
    vector<double> s1Homework, s2Homework, s3Homework;
    
    cout << "Enter the students name: ";
    cin >> s1Name;
    
    // ECT ECT
    return 0;
}


You can have a much easier time typing and keeping it organized by doing this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct students
{
    string name;
    double midterm, final;
    vector<double> homework;
};

int main()
{
    students s1;
    students s2;
    students s3;
    
    cout << "Enter your name:";
    cin >> students.name;
    
    // ECT. ECT. 

}


Now you can see when you are dealing with a large number of students (like 100 or more) it would be a lot easier to manage if all the midterm, final, name, homeworks are all in one place.

So it helps you oraganize better and saves you time. And you can do some neat things with structs like this.

So if I have a undefined number of students and needed to use the struct students for each them. I could use it in a vector like this to store each students info when the user inputs it. I removed the vector<double> homework because I did not wanna have to program another function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct student_info
{
    string name;
    double midterm, final;
};

// Function to write the grades to the struct
istream &read(istream &is, student_info &s)
{
    is >> s.name >> s.midterm >> s.final;
    return is;
}

int main()
{
    vector<student_info> students;
    student_info record;

    cout << "If you would like to stop entering the students" <<
            "info at any time just enter end of file (CRTL Z)" << endl;
    cout << endl;

        while (read(cin, record))
        {
            students.push_back(record);
        }

}


So you can see it opens up new things and also simplifies things by making it more organized and easier to work with.
Last edited on
In C++ structs and classes are the same thing minus default access. But, the convention is to use structs to hold only data, while classes hold data and methods. A common early example of a struct is this:

1
2
3
4
5
struct point
{
   int x;
   int y;
};


Now you can create an array of points for some 2D environment. Much simpler than storing x-coordinates and y-coordinates separately. Another use is if you have a function that needs to return more than one piece of information. For example, an error code and message.
closed account (zb0S216C)
Note: I'm disregarding everything already said expect the OP.

WARNING! Technical Explanation Below! WARNING!

A structure is a blueprint and an object (an instance of a structure) is a house. The structure serves as a blueprint which determines how the house (the object) is built. A structure merely groups a bunch of object/variable declarations into one place as opposed to scattered declarations.

In programming terms, a structure is a guideline for the compiler which tells it where each data-member should be placed when an object of the structure is created. Because a structure is a layout template, it does not reserve any memory by itself. In reality, a structure acts as a layer on top of memory which outlines the position of each data-member. On the lower-level, a structure groups the data-member declarations, repositions each data-member, determines the mount of alignment padding between each data-member and then determines how much memory is required in total.

When an object is created, the compiler will allocate memory for that structure and will then use the structure as a guideline so it knows where to place each data-member. The compiler will use the same structure guideline to access the data-members.

In terms of usage, there's hundreds of uses. Normally, structures are seen from different perspectives. From the object-orientated perspective, a structure represents an object, and each data-member represents an attribute of that object. From a non-object-orientated perspective, a structure is merely a group of organised bytes and each data-member is sub-group of bytes.

Wazzak
closed account (3qX21hU5)
Nice reply Framework you said it much better then I could have.
Thanks for the replies it helps a lot :)
A LOT!
Topic archived. No new replies allowed.