LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old Yesterday, 08:52 AM   #151
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,879

Original Poster
Rep: Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480

I've been re-reading the chapter about structures in the C book I downloaded, and I have some questions about the example below from it. I want to stress that the code below is exactly how it is written in the book, and thus is *not* my own code. Basically, the code below when run will ask you to enter a date, then based on the date you enter it will display the date for the next day. It doesn't seem to compile with treating warnings as errors, but should if you don't use the relevant arguments to compile it - again, it's not my code.

The first question is that; in the main function, is the following line (the very first line in the CODE block directly below) the prototype for the "dateUpdate" function? Because you'll note what as far as I can tell is the call to the same function below that, I think?

Code:
struct date dateUpdate (struct date today);
...
nextDay = dateUpdate (thisDay);
Because I've been getting quite confused trying to follow the code below. Also, is "thisDay", "nextDay", "today" and "d" the "date" structure's variables as defined in the code below?

Here's the whole code;

Code:
#include <stdio.h>
#include <stdbool.h>

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

// Function to calculate tomorrow's date

struct date dateUpdate (struct date today) {

    struct date tomorrow;
    int numberOfDays (struct date d);

   if ( today.day != numberOfDays (today) ) {
       tomorrow.day = today.day + 1;
       tomorrow.month = today.month;
       tomorrow.year = today.year;
   }
   else if ( today.month == 12 ) {
               tomorrow.day = 1;
               tomorrow.month = 1;
               tomorrow.year = today.year + 1;
   }
   else {
            tomorrow.day = 1;
            tomorrow.month = today.month + 1;
            tomorrow.year = today.year;
   }

   return tomorrow;
}

// Function to find the number of days in a month

int numberOfDays (struct date d) {

    int days;
    bool isLeapYear (struct date d);
    const int daysPerMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  
    if ( isLeapYear && d.month ==2 )
         days = 29;
    else
            days = daysPerMonth[d.month - 1];

    return days;
}

// Function to determine if it's a leap year

bool isLeapYear (struct date d) {
  
    bool leapYearFlag;

    if  (  (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0 )
          leapYearFlag = true;
    else
             leapYearFlag = false;

    return leapYearFlag;

}

int main(void) {
   
    struct date dateUpdate (struct date today);
    struct date thisDay, nextDay;

    printf ("Enter today's date (mm dd yyyy): ");
    scanf ("%i%i%i", &thisDay.month, &thisDay.day, &thisDay.year);

    nextDay = dateUpdate (thisDay);

    printf ("Tomorrow's date is: %i/%i/%.2i.\n", nextDay.month, nextDay.day, nextDay.year % 100);

    return 0;

}
Sorry if the formatting is a little out of whack, kwrite for KDE 5 keeps messing up the formatting when copying and pasting.
 
Old Yesterday, 09:57 AM   #152
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,888
Blog Entries: 13

Rep: Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379
Not sure I understand what your question is.

There is a structure defined and it is used as a return argument and also as a passing argument for that function.

Just think of it as a different variable type, such as int.

But instead of passing int, it is passing and returning that structure.

Aside: What I DON'T like is that they are using THAT as a passing argument. Use pointers instead. Structures can be huge in size. Eating stack.
 
1 members found this post helpful.
Old Yesterday, 10:10 AM   #153
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,879

Original Poster
Rep: Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480
I mean, in the main function below (for the same code above), it mentions the dateUpdate() function twice; is what's highlighted in bold below the prototype for the dateUpdate function? And where the dateUpdate() function is mentioned below that, is the actual call to call the dateUpdate() function?

Code:
int main(void) {
   
    struct date dateUpdate (struct date today);
    struct date thisDay, nextDay;

    printf ("Enter today's date (mm dd yyyy): ");
    scanf ("%i%i%i", &thisDay.month, &thisDay.day, &thisDay.year);

    nextDay = dateUpdate (thisDay);

    printf ("Tomorrow's date is: %i/%i/%.2i.\n", nextDay.month, nextDay.day, nextDay.year % 100);

    return 0;

}
And where it says struct date thisDay, nextDay; Is "thisDay" and "nextDay" the variables for the "date" structure?
 
Old Yesterday, 10:20 AM   #154
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,803

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
Yes, the bolded-line is just a declaration of the function. It could very well be outside of 'main':
Code:
struct date dateUpdate (struct date today);

int main(void) {
    struct date thisDay, nextDay;
    ...
 
1 members found this post helpful.
Old Yesterday, 06:58 PM   #155
Mechanikx
Member
 
Registered: Jul 2018
Location: Canada
Distribution: Slackware
Posts: 161

Rep: Reputation: 121Reputation: 121
Quote:
Originally Posted by jsbjsb001 View Post
... Also, is "thisDay", "nextDay", "today" and "d" the "date" structure's variables as defined in the code below?

If by "date structure's variables" you mean its members then no. The members of the date structure are bolded below:

Quote:
struct date {
int month;
int day;
int year;
};
"thisDay", "nextDay", "today" and "d" are date structures, so each one contains the three bolded members above.

Last edited by Mechanikx; Yesterday at 07:58 PM. Reason: Changed what was bolded.
 
1 members found this post helpful.
Old Yesterday, 11:38 PM   #156
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,803

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
Many of the names are confusing though, 'dateUpdate' should be 'nextDay', 'numberOfDays' should be 'daysInCurrentMonth'
 
Old Today, 03:10 AM   #157
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,879

Original Poster
Rep: Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480
Quote:
Originally Posted by Mechanikx View Post
If by "date structure's variables" you mean its members then no. The members of the date structure are bolded below:
Yeah, that's what I was thinking - after the dot, it's the structure's member variables, that's how I was trying to remember it anyway.

Quote:
"thisDay", "nextDay", "today" and "d" are date structures, so each one contains the three bolded members above.
That's pretty much what I was confused about; so "thisDay", "nextDay", "today" and "d" are actually structures, not just the variables of the "date" structure? Because this is the part that just isn't clear to me.
 
Old Today, 04:07 AM   #158
Mechanikx
Member
 
Registered: Jul 2018
Location: Canada
Distribution: Slackware
Posts: 161

Rep: Reputation: 121Reputation: 121
Quote:
Originally Posted by jsbjsb001 View Post
That's pretty much what I was confused about; so "thisDay", "nextDay", "today" and "d" are actually structures, not just the variables of the "date" structure?...
You got it.

Quote:
Originally Posted by jsbjsb001 View Post
...Because this is the part that just isn't clear to me.
Let's simplify things and just create a date structure that will hold today's date. First we create the template. This template will be used in the creation of other structures we want to be of type date.

Quote:
struct date {
int month;
int day;
int year;
};
You can't assign any values to the template because it's a declaration so there's no memory allocated for it. If we want to use a date structure we have to create one:

Quote:
struct date today;
today is now a date structure with the members month, day, year and we are able to assign values to them:

Quote:
today.month = 8;
today.day = 17;
today.year = 2019;
Does this help clear things up?

Last edited by Mechanikx; Today at 04:30 AM. Reason: Elaborated.
 
1 members found this post helpful.
Old Today, 06:31 AM   #159
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,879

Original Poster
Rep: Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480Reputation: 1480
Yeah, I think so. But just to make sure, and using the current example:

The structure is of "type date", with the three integer members "month", "day" and "year". The "today" structure is of "type date", with the same three integer members, that would look like "today->month", "today->day" and "today->year"?

Also, if I had something like below;

Code:
struct date {
int month;
int day;
int year;
struct time {
int seconds;
int minute;
int hour;
};
};

struct date today;
struct time thisDay;
It would look like "today->month", etc, etc, and for the nested "thisDay" structure of "type time", it would look like "today.thisDay->seconds", etc, etc?
 
Old Today, 10:35 AM   #160
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,888
Blog Entries: 13

Rep: Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379Reputation: 3379
I feel that it might have been best to have started a new thread some time ago.

What you say is not accurate:
Code:
struct date {
int month;
int day;
int year;
struct time {
int seconds;
int minute;
int hour;
};
};

struct date today;
struct time thisDay;

// And your comment/question:
It would look like "today->month", etc, etc, and for the nested "thisDay" structure of "type time", it would look like "today.thisDay->seconds", etc, etc?
None of those structures are pointers.

A better way to write that structure, containing another structure is the following (And note that indentation here helps, and that I've logically reordered the time elements):
Code:
struct date {
   int month;
   int day;
   int year;
   struct time {
      int hour;
      int minute;
      int seconds;
   };
};
You do not need to declare a struct time.
Code:
   struct date today; // declaration of a struct date variable name is today.

   // How you access the elements of the structure, all of them.
   today.month = 8;        // August
   today.day = 17;         // 17
   today.year = 2019;      // 2019
   today.time.hour = 11;   // 11:00
   today.time.minute = 26; // 26 minutes after the hour
   today.time.seconds = 5; // 5 seconds after the minute

   // This makes a structure of type 'date', variable named 'today' containing the data: August 17, 2019 11:26:05
If there were pointers you need to make sure said pointers are pointing to memory bolded there because I've detected this is a rather large problem.
Code:
struct time {
      int hour;
      int minute;
      int seconds;
   };

struct date {
   int month;
   int day;
   int year;
   struct time *tod;
};
For when you use that, you need to use malloc() or calloc() to declare a struct time OR you need to declare a struct time as a variable and then point to it using the "tod" pointer declared in the struct date.
Code:
   struct time now;
   struct date today;

   today.month = 8;
   today.day = 17;
   today.year = 2019;
   today.tod = &now;

   // And now you can use the -> reference, and have to because it is a pointer.
   today.tod->hour = 11;
   today.tod->minute = 26;
   today.tod->seconds = 5;
Recommend you don't play around with providing the values for the structure variables at time of declaration. I notice that you tend to do that, and get into trouble. Just advice there.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] virtualbox installs pcbsd again and again and again straffetoebak Linux - Virtualization and Cloud 4 11-21-2014 07:14 PM
LXer: Do you want a seriousóI mean seriousódeveloper laptop? Then Dell and Ubuntu have the system fo LXer Syndicated Linux News 0 11-29-2012 03:30 PM
Firefox...I have tried and tried... Basslord1124 Fedora 4 10-29-2004 11:51 PM
my ps/2's wheel doesn't work. tried and tried but failed. Choey Linux - Hardware 5 09-17-2003 06:47 PM
I have tried and tried, I really have! Ewen Linux - General 13 01-14-2003 11:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration