Monday, 20 February 2012

In code snippet below: struct Date { int yr; int day; int month; } date1,date2; date1.yr = 2004; date1.day = 4; date1.month = 12; Write a function that assigns values to date2. Arguments to the function must be pointers to the structure, Date and integer variables date, month, year?

Date is a structure with three int variables as members. set_date(..) is a function used to assign values to the structure variable.
Program: Program to illustrate a function that assigns value to the structure.
#include<stdio.h>
#include<stdlib.h>
//declare structure Date
struct Date 

{
int yr;
int day;
int month;

date1, date2;
//declare function to assign date to structure variable
void set_date(struct Date *dte, int dt, int mnt, int year) 

{
dte->day = dt;
dte->yr = year;
dte->month = mnt;
}
int main(void) 

{
date1.yr = 2004;
date1.day = 4;
//assigning values one by one
date1.month = 12;
//assigning values in a single statement
set_date(&date2, 05, 12, 2008);
//prints both dates in date/month/year format
printf("\n %d %d %d ", date1.day, date1.month, date1.yr);
printf("\n %d %d %d ", date2.day, date2.month, date2.yr);
return 0;
}

Output:
4 12 2004
5 12 2008

Explanation:
Two variables of type Date are created and named 'date1', 'date2'. 'date2' is assigned by using the function set_date(..). Address of 'date2' is passed to set_date function.