Structure in C

STRUCTURE IN C

0  0 2016-10-27 11:12:32

Structure in C is a collection of different kind of data types which are been grouped together and each of the element in C structure is called as member.
In an general terms, the composition of structure may be defined as below

struct abc
{
mem 1;
mem 2;
------------
-------------
mem m;
 }

In the above declaration, struct is an required keyword,tag is name that identifies structures of this type.

The individual members can be a ordinary variables, pointers, arrays
Or any other structures. The member names within particular structure must be distinct from each other, though member name can same as the name of variable defined outside of structure.

A storage class, however it cannot be assigned to any individual member, and individual members cannot be initialized within a structure-type declaration.

For example:
struct stud
{
char name [40];
int rollno;
float percentage;
};

we can declare the structure variable s1 and s2 as follows:
struct stud s1, s2;

s1 and s2 are structure type variables whose composition is identified by  tag stud.
It is possible to combine declaration of the structure composition with that of structure variable as shown below.

storage- class struct tag
Structures and Unions :: 281
{
mem 1;
mem 2;
-——
-—--
mem m;
} variable 1, variable 2 --------- variable n;

The tag is optional in this situation.
struct stud
{
char name [40];
int rollno;
float percentage;
}s1,s2;

The s1, s2, are structure variables of the type stud.
Since the variable declarations are now combined with declaration of the structure type, tag need not be included.
The above declaration can also be written as
struct{
char name [40];
int rollno;
float perrcentage ;
} s1, s2;
A structure may be defined as member of another structure. In such situations, the declaration of embedded structure must appear before declaration of outer structure. The members of the structure variable can be assigned initial values in much the same manner as elements of array. The initial values must appear in order in which they will assigned to their corresponding structure members, enclosed in braces and separated by the commas.
The general form is storage-class
struct tag variable = { value1, value 2,-------, value m};

A structure variable, like array can be initialized only if storage class is either external or an static.

e.g. suppose there are one more structure other than student.

struct doj
{
    int month;
    int day;
    int year;
};
struct stud
{
    char name [40];
    int rollno;
    float percentage;
    struct doj d1;
};

static struct stud st = { “ param”, 2, 99.9, 17, 11, 01};
It is also possible to define array of structure,I.e an array in which each element is structure. The procedure is shown in the below example:
struct stud
{
    char name [40];
    int rollno;
    float percentage;
} st [50];

In this declaration st is a 50- element array of structures.
It means each element of the st represents an individual student record.

 

Comments (0)

Post Your Comments

Headquarter



Hong Kong Office



Find Us On

Website Development Company
SEO
Digital Marketing
website development company in Hong Kong
website designer in pune