FoodForFutureGeeks

Wednesday 4 April 2012

Structures, Structure pointers

A structure  is a user defined datatype, it is a collection of heterogeneous data types that are supported by default. It is defined by using the keyword struct, the definition has to be terminated by a semi colon.
ex:

struct student
{
char name[15];
int age;
};
structure variables/members cannot be initialized or assigned values during structure definition.

Declaring and accessing structure variables:

structure variables can be declared just like normal integer or float variables,by mentioning the structure name followed by variable name.


#include<stdio.h>
#include<conio.h>
#include<string.h>
//structure definition

struct student
{

char name[15];
int age;
};

void main()
{
//declaring a structure variable
student s;
//accessing the structure members using . operator
strcpy(s.name,"harish");
s.age=15;
printf("student name:%s",s.name);
printf("\n student age is:%d",s.age);
_getch();

}
Structure members can be accessed by using the dot operator as shown in the sample program.

Pointer to a structure:

whenever we are working with structure variables it may be required to pass the structure variables by reference to other functions for performing operations, in such cases we need pointers to structures.Structure pointers can be declared just like ordinary pointers by using * symbol after the structure name or before variable name.
ex: student* s;

Accessing structure members from pointer:

The arrow operator(->) is used to access the structure memebers from a structure pointer, in case of ordinary structure variables we have to use the dot(.) operator to access the structure members but for structure pointer arrow operator has to be used.
ex: s->name;

Sample program to illustrate structure pointers:


#include<stdio.h>
#include<conio.h>

//structure declaration
struct student
{
char name[15];
float marks_sub1;
float marks_sub2;
float marks_sub3;
float average;
float percentile;
};

//user defined function declaration

void setData(student* temp);
void calcAvg(student* temp);
void printData(student* temp);

//main function

void main()
{

int i;

//declare an array of student type variables
student s[5];

for( i=0;i<5;i++ )
{
printf("\nenter the student %d details",i);
//set data collects the student information
setData(&s[i]);

//calculates the average
calcAvg(&s[i]);

}

for( i=0;i<5;i++ )
{

printf("\nstudent %d details",i);
//call print data to print the student details
printData(&s[i]);
}

getch();

}

//function to print data
void printData(student* temp)
{
printf("\n\n***************************************");
printf("\nname:%s",temp->name);
printf("\nsubject1 marks:%f",temp->marks_sub1);
printf("\nsubject2 marks:%f",temp->marks_sub2);
printf("\nsubject3 marks:%f",temp->marks_sub3);
printf("\nAverage marks:%f",temp->average);
printf("\n**************************************");
}

//this function calculates the average
void calcAvg(student* temp)
{
temp->average=(temp->marks_sub1+temp->marks_sub2+temp->marks_sub3)/3;
}

//this function collects the input data
void setData(student* temp)
{

printf("\nenter the name:");
scanf("%s",temp->name);
printf("\n enter the subject1 marks:");
scanf("%f",&temp->marks_sub1);
printf("\n enter the subject2 marks:");
scanf("%f",&temp->marks_sub2);
printf("\n enter the subject3 marks:");
scanf("%f",&temp->marks_sub3);
}


No comments:

Post a Comment