Array of structure and
Array of pointers to
structures
Passing Array of structure to
function
#include<stdio.h>
struct Employee
{
int id;
char name[20];
float salary;
};
struct Employee MaxSalaryEmployee(struct Employee e[], int n)
{
struct Employee m;
int i;
m = e[0];
for(i=0;i<n;i++)
{
if(e[i].salary > m.salary)
{
m = e[i];
}
}
return m;
void main()
{
struct Employee e[5], max;
int i;
for(i=0; i<5; i++)
{
printf(“Please enter id, name and salary”);
scanf(“%d%s%f”, &e[i].id, &e[i].name, &e[i].salary);
}
max = MaxSalaryEmployee(e, 5);
printf(“Employee having max salary\n”);
printf(“%d\t%s\t%f”, max.id, max.name, max.salary);
}
C Pointers
• The pointer in C language is a variable which stores the address of
another variable.
• int n = 10;
• int* p = &n;
• * (indirection operator)
• int number=50;
• int *p;
• p=&number;
• printf("Address of p variable is %x \n",p);
• printf("Value of p variable is %d \n",*p);
Pointer to structure
• Pointer to structure holds the address of the entire structure.
• It is used to create complex data structures such as linked lists, trees,
graphs and so on.
• The members of the structure can be accessed using a special
operator called as an arrow operator ( -> ).
• Declaration
• struct tagname *ptr;
• Ex: struct student *s
• Accessing
• Ptr-> membername;
• Ex: s->sno, s->sname, s->marks;
#include<stdio.h> printf("enter sno, sname, marks:");
struct student{ scanf ("%d%s%f", & s.sno, s.sname, &s. marks);
int sno; st = &s;
char sname[30]; printf ("details of the student are");
printf ("Number = %d\n", st ->sno);
float marks;
printf ("name = %s\n", st->sname);
}; printf ("marks =%f\n", st ->marks);
main ( ){ getch ( );
struct student s; }
struct student *st;
#include<stdio.h>
struct person{
int age;
float weight;
};
int main(){
struct person *p, person1; o/p- Enter age: 45
Enter weight: 60
personPtr = &person1;
Displaying:
printf("Enter age: ");
Age: 45
scanf("%d", &p->age); weight: 60.000000
printf("Enter weight: ");
scanf("%f", &p->weight);
printf("Displaying:\n");
printf("Age: %d\n", p->age);
printf("weight: %f", p->weight);
return 0;