



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
It is a notes on array in c programming language Here you can understand it and its programs How it works? How types of array and many more
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!
An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations. By using the array, we can access the elements easily. Only a few lines of code are required to access the elements of the array.
The array contains the following properties. o Each element of an array is of same data type and carries the same size, i.e., int = 4 bytes. o Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location. o Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element.
1) Code Optimization : Less code to the access the data. 2) Ease of traversing : By using the for loop, we can retrieve the elements of an array easily. 3) Ease of sorting : To sort the elements of the array, we need a few lines of code only. 4) Random Access : We can access any element randomly using the array.
1) Fixed Size : Whatever size, we define at the time of declaration of the array, we can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later.
We can declare an array in the c language in the following way.
The simplest way to initialize an array is by using the index of each element. We can initialize each element of the array by using the index. Consider the following example.
20 30 40 50 60
In the following program, we are using bubble sort method to sort the array in ascending order.
The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure. It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required.
The syntax to declare the 2D array is given below.