Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
2. Array
? An array is a collection of data items, all of the same type, accessed using
a common name.
? An array is a data structure which can hold a sequence of values.
? The array c[] holds 3 integer values:
29-10-20172
c[0]
c[1]
c[2]
-45
6
72
3. Declaring an Array
? An array is declared as-
Data-type array-name [size];
? E.g. int marks[10];
29-10-20173
Data type Array-name Array size enclosed in
brackets
? This statement defines an int array named marks that can store marks of
10 students.
?Since only a single subscript is used so it represents one-dimensional array
4. Memory allocation to an array
29-10-20174
? The storage space allocated to any array is equal to the maximum no. of
elements multiplied by the type of data type used.
? All arrays are stored in contiguous memory locations.
10
21
12
14
11
? e.g. int a[5] ;
a[0]
a[1]
a[2]
a[3]
a[4]
2 bytes
10 bytes
Representation of a array
5. Initialization of an array
29-10-20175
? Definition of an array doesn¡¯t automatically results in initialization of array
elements.
? It only allocates contiguous memory to an array with each element assigned
some garbage value.
? An array can be initialized using an equal to (=) sign.
? E.g. int a[3]={21,19,5};
A[0] a[1] a[2]
6. Types of array
? Two types-
? Linear array
? Non- linear array
? Linear array
? One dimensional arrays are called linear array.
? Only one subscript is used.
? Syntax: data-type array-name [size];
where data-types can be integer, float, double, char.
? e.g. int x[5];
float salary[20];
29-10-20176
7. Structure
? Structure is a user-defined data type in C which allows you to combine
different data types to store a particular type of record. Structure helps to
construct a complex data type in more meaningful way.
? It is somewhat similar to an Array.
? The only difference is that array is used to store collection of similar data
types while structure can store collection of any type of data.
29-10-20177
8. Defining a Structure
? To define a structure, you must use the struct statement. The struct statement
defines a new data type, with more than one member. The format of the struct
statement is as follows ?
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
29-10-20178
9. Unions
? Unions are conceptually similar to structures.
? The syntax of union is also similar to that of structure.
? The only differences is in terms of storage.
? In structure each member has its own storage location, whereas all members
of union uses a single shared memory location which is equal to the size of its
largest data member.
29-10-20179
10. Declaration
? A union is declared using union keyword.
union item
{
int m;
float x;
char c;
}it1;
29-10-201710
11. FAQ¡¯S
? How to declare array?
? How to declare 2D array?
? What is structure?
? How to declare union?
? Differentiate between structure and union?
29-10-201711