Please point out the code about how to use the structure of C language.

Asked 1 years ago, Updated 1 years ago, 43 views

The following code is a printdate function that displays the date and time entered and a Tomorrow function that returns the day after the date and time entered.Executing this code will return the wrong value (and address-like value).I think the structure was used incorrectly, but it got stuck.Please point out that the code can work properly.

#include<stdio.h>

US>structure date {
  int year, month, day;
};

void printdate(struct datep) {
  scanf("Today is %d%d%d", & (p.year), & (p.month), & (p.day));
  if(p.month<10){
    if(p.day<10){
      printf("Today is %d / %02d / %02d\n", p.year, p.month, p.day);
    } else {
      printf("Today is %d / %02d / %d\n", p.year, p.month, p.day);
    }
  } else if (p.day<10) {
     printf("Today is %d / %d / %02d\n", p.year, p.month, p.day);
  } else {
    printf("Today is %d / %d / %d\n", p.year, p.month, p.day);
  }
}

structure date tomorrow (struct date p) {
  if(p.month==1||3||5||7||8||10||12){
    if(p.day==31){
      p.month+=1;
      p.day = 1;
    } else {
      p.day+=1;
    }
  } else if (p.month==2) {
    if((p.year%4==0&p.day==29)||(p.year%4!=0&p.day==28)){
      p.month+=1;
      p.day = 1;
    } else {
      p.day+=1;
    }
  } else {
    if(p.day==30){
      p.month+=1;
      p.day = 1;
    } else {
      p.day+=1;
    }
  }
  return p;
}

int main() {
  struct date p;
  printdate(p);
  Tomorrow(p);
  printf("Tomorrow is %04d/%02d/%02d\n", p.year, p.month, p.day); 
  return 0;
}

c

2022-09-30 21:42

3 Answers

printdate(p); passes the value, not the pointer, so if you want to pass the read value to Tomorrow in printdate, you need to pass the pointer.printdate(&p);.


2022-09-30 21:42

ANOTHER QUESTION Make the code below short But what compiler do you use?
When you build the source mentioned in the question, you should receive a warning or an error message, VS2019:(The content is omitted and edited to make it easier to read)

severity code description line
Warning C6031 Return value ignored: 'scanf'8
Warning C6236 (<Equation>||<Non-zero constant>) is always a non-zero constant.   26
Warning C6001 Uninitialized memory 'p'.      58
Error C4996'scanf': This function or variable may be unsafe.8
                 Consumer using scanf_sinstead.
                 To disable deprecation, use_CRT_SECURE_NO_WARNINGS.
                 See online help for details.

Just by looking at and dealing with these questions before you ask them, you can reduce the number of comments and responses you make.
However, please note that if you do not consider the method and order of action, you may proceed even though the action is not complete.

For example, to prevent error C4996 (not really good)However, if you enter #pragma warning (disable:4996), the error changes to the following:
This is similar to @Uncle-Kei's comments and @Wawai's answers

severity code description line
ERROR C4700 Local variable 'p' not initialized is used 58

Therefore, if you initialize p as shown below, the build will be completed.

structure datep={};

The following warning remains, but you can proceed and debug it, which can lead to incomplete actions.
Second Warning C6236 is the same as @metropolis' third comment

severity code description line
Warning C6031 Return value ignored: 'scanf'8
Warning C6236 (<Equation>||<Non-zero constant>) is always a non-zero constant.   26

Now that you've come to this point, there are still issues that won't get caught in compiler warnings or errors.
This is the first and second comment from @metropolis

Therefore, the main issues are not "how to use the structure" but are as follows:

  • Is the variable initialized
  • Are the parameters passed to the function and received the results of the function correct?
  • Understand when data will be entered and generated, and where they will be used (transmitted)
  • Does each program have its intended results and effects?

Check the program with @int32_t's comments in mind

add
I don't know if the question is a challenge/self-study, and it's none of your business, but if I were to say rock-paper-scissors, the date-time structure is standard in C language.

C Language Function Dictionary-tm Structure
C Language Function Dictionary-time.h

By the way, functions that specify one date and return the next are not defined in the standard.
However, since it has been used for a long time, there are many examples of such programs.

Date/Time Action

If you're asking me to start from scratch, I think it's more efficient to think about how to use these standards if it's self-study.

Other languages come standard with more features than C, so if you can afford it, you might want to look at it and think of your own library to fill the gap with other languages.
C++ Date and Time Utility
.NET DateTime Structure
Python datetime --- Basic date and time
Java SE 8 Date and Time


2022-09-30 21:42

structure date p;
printdate(p);

For C language, the local variable contains a false value.
I didn't substitute anything, so the value of that nonsense will be printed.

Next,

tomorrow(p);
printf("Tomorrow is %04d/%02d/%02d\n", p.year, p.month, p.day); 

However, this does not change the value of p
The arguments of a function are copied and passed to the function, so no matter how many values you change in the function, they are not reflected in the caller.
In the end, even in this printf statement, the contents of p are printed as nonsense


2022-09-30 21:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.