Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

structures

Posted on 1999-07-26
6
Medium Priority
?
250 Views
Last Modified: 2010-04-15
I have an assignment for C, I have to write a program that has structures defined,the first structure is called struct date,and has int yy,mm,dd. I have to writet a function that will ask for the date in numerical format. This function must return the date (namely int yy,mm,dd) to struct date.
 
1. The function should be called get_date() and must not receive anything. Do have to make an integer in the function. If I do this will read as follow:scanf("%d %d %d",&yy,&mm,&dd);
and if it does how do i return this to struct date??
0
Comment
Question by:jonasw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:jonasw
ID: 1263942
Edited text of question.
0
 

Author Comment

by:jonasw
ID: 1263943
Edited text of question.
0
 

Accepted Solution

by:
paitre earned 400 total points
ID: 1263944
1. It's kind of hard to understand your English, but I'll give it a go.  The easiest way to do this would be to have a variable of the DATE structure local to your get_date() func, and have get_date() have a retval of type DATE.

ie:

typedef struct date {
      int day, month, year;
} DATE;

DATE get_date( void )
{
      DATE today;

      scanf( "%d/%d/%d\n", &today.year, &today.month,                   &today.day );
      return today;
}

int main ( int argc, char *argv[] )
{
      DATE today;

      printf( "Enter a date: " );
      today = get_date();

      ...
      
      return 1;
}

Code has been checked to see if it works as written, and does using egcs 1.1.2 under Linux 2.0.36.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:Triskelion
ID: 1263945
/*Here's an example with pointers*/
#include <stdio.h>

typedef      struct      DateStruc
{
      int      yy; /*year */
      int      mm; /*month*/
      int      dd; /*day  */
} DateStruc;

DateStruc * get_date(void);

void main(void)
{
      DateStruc * xpDate=get_date();
      printf("%02d/%02d/%02d", xpDate->mm, xpDate->dd, xpDate->yy)      ;
}

DateStruc * get_date(void)
{
      static      DateStruc      xDate;
      printf("Please enter the date in the following format {mm/dd/yy}\r\n");
      scanf("%2d/%2d/%2d", &xDate.mm, &xDate.dd, &xDate.yy);
      return      &xDate;
}
0
 

Expert Comment

by:paitre
ID: 1263946
I thought about doing pointers, but since he was asking for a struct explanation/example, I did it that way as simply as I could :)
Pointers work well for this kind of thing, though.
0
 

Author Comment

by:jonasw
ID: 1263947
thanks paitre

sorry about the use of my english, i was at work and was doing 101 things at the same time as well as typing the question. it is not usually that bad but i read it again now and saw what you meant. it is pretty bad.
thanks for your response, you really helped me.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question