Jump to content

Structures

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
josh

josh

    Newbie

  • Members
  • Pip
  • 3 posts
Structure is used in c language to group elements together. For eg we wish to store a date say for today 31/3/2010, we can assign 3 integer datatype, day which is hear '31', month which is '3', year which is '2010'.
it can be written as

int day=31, month=3, year=2010;

if we want to keep the purchase details of a product with dates like for eg purchasing day, purchasing month, purchasing year so we need three more variables to declare like

int purchasing_day, purchasing_month, purchasing_year;

To keep the record of dates we need to access the above variables. Since they are related logically it would be good to have these accessed relatively. Here we make use of structures.
We can define a stucture named date consisting of three components namely day, month and year. The general syntax is

struct date

{

         int day;

         int month;

         int year;

};

Now we can declare a new type in date declared in the structure date as

struct date today;

We need to use a special syntax to deal with structure variables. Structure members which includes the variables are accessed by . operator as

today.day=5;

today.month=5;

today.year=1998;


#2
spyder

spyder

    Programmer

  • Members
  • PipPipPipPip
  • 120 posts
Just a tidbit of constructive criticism.
You should also include information about pointers to functions in structures and typedefs.:)