U.S. CitiesCity-Data Forum Index
Go Back   City-Data Forum > General Forums > Science and Technology > Computers
 [Register]
Please register to participate in our discussions with 2 million other members - it's free and quick! Some forums can only be seen by registered members. After you create your account, you'll be able to customize options and access all our 15,000 new posts/day with fewer ads.
View detailed profile (Advanced) or search
site with Google Custom Search

Search Forums  (Advanced)
 
Old 07-19-2018, 01:58 PM
 
171 posts, read 72,652 times
Reputation: 97

Advertisements

I am currently teaching myself the C language, and I have made it to Structures.
However, I am not sure why exactly I would want to use that tool, because it doesn't seem to make things any more convenient.
It claims to make things more "compact" and to "group things together" and even compares itself to arrays (which were absolutely very convenient), but so far I cannot see any real difference.
Here is an example of some half-decent code that is used to demonstrate Structures, and it is supposed to find the next day's date (it ignores leap years and it doesn't correct anything, but it's good enough - note that the scanf() function doesn't work on the online C program that I am currently using):

Spoiler
#include <stdio.h>

int main()
{
struct date
{
int year;
int month;
int day;
};

struct date today, tomorrow;

today.year = 2018;
today.month = 7;
today.day = 19;

int DaysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if(today.day != DaysPerMonth[today.month - 1])
{
tomorrow.year = today.year;
tomorrow.month = today.month;
tomorrow.day = today.day + 1;
}
else if(today.month == 12)
{
tomorrow.year = today.year + 1;
tomorrow.month = 1;
tomorrow.day = 1;
}
else
{
tomorrow.year = today.year;
tomorrow.month = today.month + 1;
tomorrow.day = 1;
}

printf("%d/%.2d/%.2d", tomorrow.year, tomorrow.month, tomorrow.day);

return 0;
}


However, I could easily change this code into an equivalent program that didn't use Structures at all, and this even made the program slightly shorter:

Spoiler
#include <stdio.h>

int main()
{
int today_year = 2018;
int today_month = 7;
int today_day = 19;
int tomorrow_year;
int tomorrow_month;
int tomorrow_day;

int DaysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if(today_day != DaysPerMonth[today_month - 1])
{
tomorrow_year = today_year;
tomorrow_month = today_month;
tomorrow_day = today_day + 1;
}
else if(today_month == 12)
{
tomorrow_year = today_year + 1;
tomorrow_month = 1;
tomorrow_day = 1;
}
else
{
tomorrow_year = today_year;
tomorrow_month = today_month + 1;
tomorrow_day = 1;
}

printf("%d/%.2d/%.2d", tomorrow_year, tomorrow_month, tomorrow_day);

return 0;
}


What am I missing here?
Am I supposed to be able to call all of the information from "today" and "tomorrow" at once simply by referring to those variables in some way?
Or maybe the Structure tool makes it easier for C to deal with all the members and thus conserves memory or something?

Please advise.
Reply With Quote Quick reply to this message

 
Old 07-19-2018, 04:44 PM
 
Location: Wandering.
3,547 posts, read 5,683,767 times
Reputation: 2664
Structs or Classes in their most basic form are just containers that group a set of variables (members) together that represent a single object.

They really start to make sense when you have dozens, hundreds, or even thousands of objects to deal with, and especially when the objects get more complicated than just a couple of variables.

What if you need to deal with an entire years worth of dates, not just today or tomorrow. Writing those variables out in your code would become a crazy mess (and you'd always have a limitation on how many you could deal with at any point in time). Instead you can create a new array of the date struct, add all of the ones you need to deal with to the array, and then you can work with each date as a single item.

And yes, in the example where you have two "date" objects (today and tomorrow) you can reference all of the info in the struct by using it's variable name (it's a container variable, that has member variables).


At a larger scale, think about creating a tool for managing employees. Each employee has a name (or two), phone, address info, email, birthday (a date struct), etc. This can rapidly became very large and unmanageable without some sort of container to "hold" all of those properties. I don't remember if straight C structs can contain code or structs, but in most OO languages structs can contain their own code and sub structs to further organize and containerize code and data.


As far as memory goes, generally you'll actually have a small amount of memory overhead for creating / managing the struct itself, so they don't conserve any memory. The trade off is clean code and flexibility in exchange for a tiny amount of memory.
Reply With Quote Quick reply to this message
 
Old 07-19-2018, 10:31 PM
 
Location: Jakarta
68 posts, read 24,129 times
Reputation: 85
If you have many variables, struct is useful

Quote:
#include <stdio.h>

struct abc {
int a,b,c;
};

int main() {
struct abc x;
x.a = 1, x.b = 2, x.c = 3;
printf("%d %d %d %d %d %d",x,x,x,x,x,x); // 1 2 3 1 2 3
return 0;
}
Reply With Quote Quick reply to this message
 
Old 07-20-2018, 11:12 AM
 
3,000 posts, read 3,131,568 times
Reputation: 2910
Structures are more extensible than individual variables. It also one of two ways to pass variables back from a function (the other method being to pass a pointer)

Quote:
struct date
{
int year;
int month;
int day;
};


int main()
{....
date temp; temp.year = 2018; temp.month = 7; temp.day=20;
temp = Add180Days(temp);
}

date Add180Days(date value)
{
//Insert whatever code to add 180 days
//code here
return value;
}
Since when you add 180 days (in this example), the year, month and day would all change, you'd need a way to pass those back. You could always pass by reference, but since you know that these 3 variables always go together, why not wrap them up in a structure instead? To be fair, you could also pass the structure in by reference as well. But, I'm just trying to give an example.
Reply With Quote Quick reply to this message
 
Old 07-23-2018, 08:23 AM
 
171 posts, read 72,652 times
Reputation: 97
Thanks.

I found another example a couple days ago where a program passed an entire structure into a function because it wanted to make "if"-tests on some of the variables in that structure.
I guess that's much more convenient than passing every variable separately, lol.
Reply With Quote Quick reply to this message
Please register to post and access all features of our very popular forum. It is free and quick. Over $68,000 in prizes has already been given out to active posters on our forum. Additional giveaways are planned.

Detailed information about all U.S. cities, counties, and zip codes on our site: City-data.com.


Reply
Please update this thread with any new information or opinions. This open thread is still read by thousands of people, so we encourage all additional points of view.

Quick Reply
Message:

Over $104,000 in prizes was already given out to active posters on our forum and additional giveaways are planned!

Go Back   City-Data Forum > General Forums > Science and Technology > Computers
Follow City-Data.com founder on our Forum or

All times are GMT -6. The time now is 03:47 PM.

© 2005-2019, Advameg, Inc. · Please obey Forum Rules · Terms of Use and Privacy Policy · Bug Bounty

City-Data.com - Archive 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, 35 - Top