LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 08-16-2019, 08:52 AM   #151
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063

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 08-16-2019, 09:57 AM   #152
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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 08-16-2019, 10:10 AM   #153
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
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 08-16-2019, 10:20 AM   #154
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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 08-16-2019, 06:58 PM   #155
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
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; 08-16-2019 at 07:58 PM. Reason: Changed what was bolded.
 
1 members found this post helpful.
Old 08-16-2019, 11:38 PM   #156
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Many of the names are confusing though, 'dateUpdate' should be 'nextDay', 'numberOfDays' should be 'daysInCurrentMonth'
 
Old 08-17-2019, 03:10 AM   #157
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
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 08-17-2019, 04:07 AM   #158
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
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; 08-17-2019 at 04:30 AM. Reason: Elaborated.
 
1 members found this post helpful.
Old 08-17-2019, 06:31 AM   #159
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
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 08-17-2019, 10:35 AM   #160
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
Old 08-17-2019, 10:53 PM   #161
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by rtmistler View Post
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;
   };
};
Not sure this makes sense, I get a warning when compiling it:

Code:
tmp.c:9:5: warning: declaration does not declare anything
    };
     ^
 
1 members found this post helpful.
Old 08-17-2019, 11:49 PM   #162
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
Quote:
Originally Posted by ntubski View Post
Not sure this makes sense, I get a warning when compiling it:

Code:
tmp.c:9:5: warning: declaration does not declare anything
    };
     ^
I got the same warning so after trying a couple of things and failing I googled it and found this on stack overflow:

https://stackoverflow.com/questions/...structure-in-c

and it worked:

Code:
struct date {
    int month;
    int day;
    int year;
    struct time {
        int hour;
        int minute;
        int seconds;
    } time_current;
};
 
1 members found this post helpful.
Old 08-17-2019, 11:51 PM   #163
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Quote:
Originally Posted by rtmistler View Post
I feel that it might have been best to have started a new thread some time ago.
...
I figured that structures was relevant, that's why I continued with this thread. But that said, if the mod's feel the posts after post #131 (or whatever other post) should be split off into it's own thread, I don't have any issues with that - so I'll leave it up to the mod's/LQ to decide. Thanks for that post of yours tho RT, it was very helpful.

Anyway, I've been doing a little practice with nested structures, and I think I've got it. I had a look at both the C book and this site, and I've written some code that compiles without any warnings or errors. It just display's today's date (for where I live) and the time when I was writing the same code (I haven't used any pointers in it tho). But I'm still getting use to how to properly indent struct statements/structures tho, so sorry if my indenting is a little off (and kwrite keeps messing up my formatting when I copy and paste any code - written by myself or not).

Code:
#include <stdio.h>

int main(void) {

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

    struct date today;
    
    today.day = 18;
    today.month = 8;
    today.year = 2019;
    today.now.hour = 1;
    today.now.minute = 51;
    today.now.seconds =  32;

    printf("Date and time is: %i/%i/%i %i:%i:%i\n", today.day, today.month, today.year, today.now.hour, today.now.minute, today.now.seconds);

    return 0;
}
 
Old 08-18-2019, 07:22 AM   #164
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by jsbjsb001 View Post
kwrite keeps messing up my formatting when I copy and paste any code - written by myself or not
Could that be a due to a difference in tab size?
 
Old 08-18-2019, 09:31 AM   #165
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by ntubski View Post
Could that be a due to a difference in tab size?
Or perhaps the presence of tabs entirely. I suggest to never use TABS, except in a Makefile where they are required, or any other particular file similar in nature. C source files, I feel should all contain spaces as opposed to tabs.

Just my humble opinion.

And sorry once again for posting code, which was not compiled, just typed off the cuff. Obviously caused a bit of confusion there.
 
  


Reply



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 06:32 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
Open Source Consulting | Domain Registration