EmbLogic's Blog

Structures

Structure is a user defined data type which is a collection of different elements of different data types stored at a continuous memory location.

Syntax for structure is:

struct name

{

data-type element;

data_type element;

………;

………;

};

struct name variable;

structure function is always declared globally. struct name variable can be written inside the scope of the main function().

eg:                  struct distance

{

int feet;

float inches;

};

struct distance d;

Here d is the variable of struct distance type. And the memory is automatically given to d variable. then inside the main function, value given to feet and inches will b as shown:

int main()

{

d.feet=5;

d.inches=6.1;

printf(“distance in feet is:%d and in inches:%d\n”,d.feet,d.inches);

}

If variable d is a pointer of struct distance type,then the memory is not allocated directly to the d pointer. then inside main function() the value to feet and inches will be given as follows:

int main()

{

d->feet=5;

d->inches=4.1;

printf(“distance in feet:%d and inches:%d\n”,d->feet,d->inches);

}

when variable is declared of struct distance type then accessing the value is in the form d.feet and d.inches,where ” . ” represents the scope resolution operator.

When pointer is declared of struct distance type then accessing the value is in the form of d->feet and d->inches,where ” . ” is changed to ” -> “.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>