0% found this document useful (0 votes)
1 views

11-array

An array is a collection of elements of the same data type, allowing for the storage of multiple values in a single variable. Each element is accessed by its index, starting from 0, and arrays must be declared with their type and dimensions before use. In C, there is no built-in bounds checking, making it the programmer's responsibility to ensure that array indices do not exceed the allocated size.

Uploaded by

ashutoshraghav01
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

11-array

An array is a collection of elements of the same data type, allowing for the storage of multiple values in a single variable. Each element is accessed by its index, starting from 0, and arrays must be declared with their type and dimensions before use. In C, there is no built-in bounds checking, making it the programmer's responsibility to ensure that array indices do not exceed the allocated size.

Uploaded by

ashutoshraghav01
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Arrays

What is an Array
• Ordinary variables are capable of holding only one value at a time.
• However, there are situations in which we would want to store more than
one value at a time in a single variable.
• Suppose we wish to arrange the percentage marks obtained by 100
students in ascending order. In such a case we have two options to store
these marks in memory:
– Construct 100 variables to store percentage marks obtained by 100 different students,
i.e. each variable containing one student’s marks.
– Construct one variable (called array or subscripted variable) capable of storing or holding
all the hundred values.
What is an Array
• An array is a collective name given to a group of elements of the same
data type.
• These similar elements could be all ints, or all floats, or all chars, etc.
• Each member in the group is referred to by its position in the group. C the
counting of elements begins with 0 and not with 1.
A Simple Program Using Array
What is an Array: Summary
• An array is a collection of similar elements.
• The first element in the array is numbered 0, so the last element is 1 less
than the size of the array.
• An array is also known as a subscripted variable.
• Before using an array its type and dimension must be declared.
• However big an array its elements are always stored in contiguous
memory locations.
Array Initialization
• int num[6] = { 2, 4, 12, 5, 45, 5 } ;
• int n[ ] = { 2, 4, 12, 5, 45, 5 } ;
• float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ;

• Till the array elements are not given any specific values, they are supposed
to contain garbage values.
• If the array is initialized where it is declared, mentioning the dimension of
the array is optional.
Array Elements in Memory
• int arr[8] ;

• When we make this declaration, 16 bytes get immediately reserved in


memory, 2 bytes each for the 8 integers.
• The array is not being initialized, all eight values present in it would be
garbage values. This so happens because the storage class of this array is
assumed to be auto.
• If the storage class is declared to be static then all the array elements
would have a default initial value as zero.
• Whatever be the initial values, all the array elements would always be
present in contiguous memory locations.
Bounds Checking
• In C there is no check to see if the subscript used for an array exceeds the
size of the array.
• Data entered with a subscript exceeding the array size will simply be
placed in memory outside the array; probably on top of other data, or on
the program itself.
• There will be no error message to warn you that you are going beyond the
array size. In some cases the computer may just hang.
• Thus, to see to it that we do not reach beyond the array size is entirely the
programmer’s botheration and not the compiler’s.

You might also like