Structure Function Array Pointer

Structure and Function

Structures can also be passed as a parameter to a function. This is a powerful feature that greatly simplifies the use of functions thus writing of well-constructed modular programs that use structure.

```struct student getdata( );
void list ( struct student);
struct student
{
char name[30];
int no;
};
void main(void)
{
struct student s1=getdata();
struct student s2=getdata();
list(s1);
list(s2);
}
struct student getdata()
{
struct student temp;
gets(temp.name);
printf(“Enter Roll No  :”);
scanf(“%d”,&amp;temp.no);
return(temp);
}
void list(struct student temp)
{
printf(“ Name = %s”, temp.name);
printf(“ Roll No = %d”, temp.no);
}

```

Array of Structures

A structure may occur as an array element. Example:

```struct student
{
char id[9];
char name[30];
int mark;
};

int main(void)
{
struct student stulist[100];
...
}
```

Nested Structure

A nested structure is sometimes called the structure of the structure.

```#include <stdio.h>
struct date
{
int day;
int month;
int year;
};
struct book
{
char author[30];
char title[50];
char publisher[30];
int edition;
struct date date_of_pub;
};
int main(void)
{
struct book  booklist[100];
strcpy(booklist[10].author, "Al Kelly");
strcpy(booklist[10].title, "C By Dissection");
booklist[10].edition = 3;
booklist[10].date_of_pub.day = 1;
booklist[10].date_of_pub.month = 2;
booklist[10].date_of_pub.year = 1996;
return 0;
}
```

Pointer to Structure

Example 1

```struct date
{
int day;
int month;
int year;
};
void  main(void)
{
struct date today;
struct date *ptrDate;
ptrDate=&today;

(*ptrDate).day = 25;
(*ptrDate).month = 12;
(*ptrDate).year = 1997;

if (today.day==1 &amp;&amp; today.month==1)
printf(“Happy New Year !\n");
}

```

Example 2

```struct date
{
int day;
int month;
int year;
};
void  main(void)
{
struct date today;
struct date *ptrDate;
ptrDate=&today;

ptrDate->day = 25;
ptrDate->month = 12;
ptrDate->year = 1997;

if (today.day==1 &amp;&amp; today.month==1)
printf(“Happy New Year !\n");
}

```