Saturday, 24 June 2023

Data Structure in C

What is Data Structure?

Consider a plan or program to work on the data of students in a school. Each students record would have definite certain fields namely naming or identity number, student name, class etc. From what we have take in or learnt so far we could use a structure to summarize or encapsulate these fields. We store these data or records in files, so we could use structure flexible or variables to read a record from a file or write a data or record to a file.

Data Structure, c language, C program
Data Structure Image-1

If we wanted to operate or manipulate several data or records at once we would need an array of structures. Say if we wanted to show, all the students data or records in the order of their naming or identity numbers we would need to read them from the file they are keep or stored into an array of structures.

This is because the C language does not offer any file company or organization methods other than the uninterrupted or sequential file operation or organization. In other words when you write data or records into a file in the C language you write the data or records in a sequence. The language does not offer any operation or organization such as sign or index File Company or organization where the data or records can be read or accessed at random on a key value.

The fseek function does allow random read or access to file data but only based on the byte balance or offset. Therefore we first have to access or read them into an array of structures, sort the array by the key area or field, and display each element of the array.

Here is where we can see the issue or problem with arrays during coding or programming. The question, which appear or arises, is what should the size of the array? Obviously or clearly our array must have at least as many elements or components as the biggest or maximum number of records we plan or intend reading from the file.

Now, if we utilize or use an array that is too big (say there are only five hundred data or records in the data file and the array size we have put or set, to read or access these data or records into, is one thousand elements or components long) every time we run the plan or program we are going to set aside or reserve memory for one thousand structures but use only five hundred.

Worse or poor would be the case if the number of students in the school grow or increases to over a thousand. Then we would have to change the plan or program code and grow or increase the army size and keep doing so forever.


Computer stuff kit tricks of Topics 66.