structure in c

11
Structure Structures are user defined data types It is a collection of heterogeneous data It can have integer, float, double or character data in it We can also have array of structures struct <<structname>> { members; }element; We can access element.members;

Upload: prabhu-govind

Post on 13-Dec-2014

155 views

Category:

Education


0 download

DESCRIPTION

Structure in c S-Teacher

TRANSCRIPT

Page 1: Structure in c

StructureStructures are user defined data typesIt is a collection of heterogeneous dataIt can have integer, float, double or character

data in itWe can also have array of structures

struct <<structname>>{

members;}element;

We can access element.members;

Page 2: Structure in c

Example

struct Person{

int id;char name[5];

}P1;P1.id = 1;P1.name = “vasu”;

Page 3: Structure in c

typedef statementUser Defined Data Types The C language provides a facility called typedef

for creating synonyms for previously defined data type names. For example, the declaration:

   typedef int Length; 

makes the name Length a synonym (or alias) for the data type int.

Page 4: Structure in c

typedef(contd.)The data “type” name Length can now be

used in declarations in exactly the same way that the data type int can be used:

         Length a, b, len ;          Length numbers[10] ;

Page 5: Structure in c

UNION

Page 6: Structure in c

UNIONUnion has members of different data types,

but can hold data of only one member at a time.

The different members share the same memory location.

The total memory allocated to the union is equal to the maximum size of the member.

Page 7: Structure in c

EXAMPLE#include <stdio.h>

union marks{    float percent;    char grade;};int main ( ){    union marks student1;    student1.percent = 98.5;    printf( "Marks are %f   address is %16lu\n", student1.percent, &student1.percent);    student1.grade = 'A';    printf( "Grade is %c address is %16lu\n", student1.grade, &student1.grade);}

Page 8: Structure in c

ENUM

Page 9: Structure in c

ENUMERATED DATATYPE Enumeration is a user-defined data type. 

It is defined using the keyword enum and the syntax is:

   enum tag_name {name_0, …, name_n} ; 

The tag_name is not used directly. The names in the braces are symbolic constants that take on integer values from zero through n. 

Page 10: Structure in c

Enumerated(contd.)

As an example, the statement:        enum colors { red, yellow, green } ; creates three constants.  red is assigned the value 0, yellow is assigned 1 and green is assigned 2.

Page 11: Structure in c