Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array, Structure and Union

Similar presentations


Presentation on theme: "Array, Structure and Union"— Presentation transcript:

1 Array, Structure and Union

2 Array, Structure and Union
Group of related data items which shares a common name More than one items, with same name and same data type If we need more variables of same type, then we can use array An array can be declared as int a[10]; a integer array with 10 elements data type array name [size];

3 Array, Structure and Union
Each elements in the array can access using its relative position, called index of the array or subscript of the array Index of first element is 0, second element is 1 and so on int a[10]; Index a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] 10 20 30 40 50 60 70 80 90 100

4 Array, Structure and Union
Each elements in the array can be access using a loop Reading an array for(i=0;i<n;i++) { scanf(“%d”,&a[i]); } Printing an array printf(“%d “,a[i]); Where nsize of the array

5 Array, Structure and Union
A structure is a collection of one or more variables, possibly of different types, grouped together under a single name Grouping logically related data, and giving this set of variables a higher-level name .

6 Array, Structure and Union
A structure is defined by the keyword struct followed by a set of variables enclosed in braces. Consider the following structure to represent a person’s details. struct Personnel { char name[100]; int age; double height; }; The variables name, age and height are called members of the structure type Personnel.

7 Array, Structure and Union
There are two ways to define variables of a particular structure type. Declare them at the structure definition. struct Personnel { char name[100]; int age; double height; } p1, p2, p3; /* Define 3 variables */ Define the variables at some point after the structure definition. struct Personnel p1, p2, p3; /* Define 3 variables */

8 Array, Structure and Union
Union is same as the structure Like structure, union also contains members of different data type Union can be declared using the keyword union Syntax: union unionname{ member 1; member 2; }; Like structure a union variable can be declare Syn: union unionname variable list; popo

9 Array, Structure and Union
The memory space occupy for a union variable is the size of largest member In structure each members has its own memory locations In union all members shares a common memory location In structure we can access all members at a time In union we can access only one member at a time popo

10 Array, Structure and Union
Eg Stuct check { int age; char name[5]; }s; The memory space of struct variable s=sum of size of all members Ie sizeof(age)+sizeof(name) => 2+5 = 7 bytes The structure variable has separate memory locations for each members Can access all members at a time age (2 bytes) name (5 bytes) popo

11 Array, Structure and Union
Eg union check { int age; char name[5]; }u; The memory space of union variable s=size of largest members Ie sizeof(name) => 5 = 5 bytes The union variable has only one memory locations for all members Can access only one member at a time name (5 bytes) popo


Download ppt "Array, Structure and Union"

Similar presentations


Ads by Google