Thread: Advantages in struct?

  1. #1
    Registered User
    Join Date
    Jul 2003

    Advantages in struct?

    This was an exercise comparing to methods of holding month/days of month data and displaying them. Which would you use and why?

    The exercise is from C++ Programming Language 3rd edition 5.9 #7, pg 105.

    // - to compare an array of structs to an array of cstrings
    //           holding  month/day of month data
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    int main(int argc, char *argv[])
      const char *months[12] = {"January","February","March","April",
      int daysofmonths[12]={31,28,31,30,31,30,31,30,30,31,30,31};
      struct monthdays{
        char month[10];
        int days;
      monthdays year[12]={"January",31,"February",28,"March",31,"April",30,
      for (int index=0; index < 12; ++index)
          cout << months[index] << "\t\t\t" << daysofmonths[index] << endl;
      for (int index=0; index < 12; ++index)
          cout << year[index].month << "\t\t\t" << year[index].days << endl;
    return 0;
    forgot to declare everything as const oh well
    Last edited by curlious; 12-14-2003 at 01:50 AM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    At this point in your learning, don't worry about it. The exercise(and chapter?) are introducing you to the mechanics of how structs work. The next step is learning about classes(which except for one minor detail are identical to structs) and their use in object oriented programming, where you will soon discover their unique properties.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Yes, I see no advantage in the struct in the given example. It doesn't even encapsulate the data because default access is public.

    The code is no easier to read and the exercise specified char array as apposed to the string class.

    Perhaps it is just a mechanical exercise, though I felt he wanted some type of comparison. He doesn't explicitly ask this though.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Try calling a function or two

    do_something ( month, days )
    do_something ( monthdays )

    Now imagine you had 20 related items
    The first call would have 20 params all of a sudden, yet the 2nd one would remain at 1 parameter
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Good point!
    In such a case a struct would be neater even in the simple example you illustrated the structure is eazier.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    The time_t has never been beat, I say go with that. You can even derive a new structure from the tm time struct [time.h, ctime].
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. Tutorial review
    By Prelude in forum A Brief History of
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM