Closed Thread
Results 1 to 9 of 9

Thread: What is the point of structures?

  1. #1
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    What is the point of structures?

    I read about structures when learning C and I haven't used them since. What is the point? They just give variables names like structure.variable. I would rather keep using simple variable names like a,b,c etc. (That is usually what I do.)
    I guess they can make things a little more organized, but there has to be a better reason. My book just says they group variables together. So? Who cares? Please give me a good reason for using structures in C programming.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    ahmed is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    300
    Rep Power
    0

    Re: What is the point of structures?

    I will try to make clear for you with an example .
    Suppose there's a program in which you want to store record of 50 students with their name,roll no, what will you do ? Make separate variables for every single student name and roll no , wont it take ages lol ? With structures you won't have that such problem , make a structure of student and use it again and again , which will in fact save a lot of your time

  4. #3
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: What is the point of structures?

    I'd think Guest would like a real-world use. Well, besides structures being the basis of OO programming (classes are nothing more than structures with functions and permission control, really), they in themselves have the quite utilitarian purpose of storing multiple values within one object. This makes it so in every literal sense you can return multiple values from a function without needing to use pointers (or for that matter any function parameters).

    Struct's primary advantage, though, comes with organization of ideas. It's much easier to look at an organized map as a set of connected arrays and an integer rather than just two free floating arrays and an integer. As an example, I wrote a C program for a client, and it required a structure similar to a std::map, the way it was done was with a char* array and an int array, as well as an integer to tell me how many members the struct had. This made using this struct as a return value much easier, and allowed for more modular and reusable code.
    Wow I changed my sig!

  5. #4
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: What is the point of structures?

    Structures can make your code a lot easier to read and maintain.

    Using ahmeds example of studends you could declare the structure as follows
    Code:
    struct student
    {
        char name[30];
        int rollNumber;
    };
    then each student's name and rollNumber can be accessed as student1.name and student1.rollNumber.

    If you were to use individual variables for each student you'd have to declare each one seperately and have some way of linking the two together. You'd end up with somthing like
    Code:
    char student1Name[30];
    int student1RollNumber;
    which is fine if you have just one student to deal with, but, if you have 50 to deal with the amount of additional code will increase (you'll have to declare two variables instead of one) which will increase the chance of errors appearing in your code.

    Now consider an instance where you need to hold several peices of information about an item. Say you want to record information about books and you want to store title, author, ISBN, number of pages, price etc. By holding these in a structure you are ensuring that all the relevant data stays together while at the same time significantly reducing the amount of code you have to write and maintain.
    If there's a new way, I'll be the first in line.

    But, it better work this time.

  6. #5
    oliver.rush's Avatar
    oliver.rush is offline Newbie
    Join Date
    Sep 2009
    Posts
    24
    Rep Power
    0

    Re: What is the point of structures?

    Adding one more point to the thread: it's not a really good idea to use variables called a, b, c... you may understand them now, but imagine a code with 500 lines or more, which you have to change after 3 years you finished it. Do you think you're going to remember what they mean?

    Try to use names with some meaning. It's a good habit.

  7. #6
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: What is the point of structures?

    Ultimate reason: you need structures to build things like linked lists.
    If you have a structure, you are defining a new data type that can include a pointer to itself. This allows you to chain the dynamic allocation of memory for arbitrary amounts of data storage.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    h4x Guest

    Re: What is the point of structures?

    just a block of memory you can access easy way.
    you have labels instead of offsets.

  9. #8
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: What is the point of structures?

    Thanks guys! I can see what you mean, all of you have provided valid uses of structures. The thing I think will be interesting is functions that return structures.
    @WingedPanther: Your point is especially interesting
    @oliver.rush: I usually have comments that say what the variables mean, I just don't like typing out long variable names all the time. Thanks for the tip though, I don't have good programming habits. ( Something I should improve upon)
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  10. #9
    fantanoice's Avatar
    fantanoice is offline Newbie
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Re: What is the point of structures?

    Thanks guys! I can see what you mean, all of you have provided valid uses of structures. The thing I think will be interesting is functions that return structures.
    Well, I'm currently doing an assignment which returns returning a struct in a function.

    Basically, it involves building a battleships game. In this instance, returning the struct will return the new gameboard (squares, boats, etc) to be printed to the screen.


    Just as an example.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Constants - what's the point?
    By justanothernoob in forum C# Programming
    Replies: 9
    Last Post: 04-09-2011, 01:29 AM
  2. Point of sales HELP!
    By teddiorsenado in forum C# Programming
    Replies: 2
    Last Post: 12-05-2010, 12:53 PM
  3. Point me in the right direction please! WM6
    By jamlid in forum C# Programming
    Replies: 1
    Last Post: 02-12-2010, 10:33 AM
  4. In need of floating point help
    By Ansems_keyblade in forum General Programming
    Replies: 3
    Last Post: 12-02-2009, 10:02 AM
  5. Is there a point?
    By Void in forum Shareware Sites
    Replies: 10
    Last Post: 11-28-2006, 09:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts