Structures And User Defined Data Types (Part – I)

Howdy folks, hope you’re doing well. We’ve already discussed about 1D Arrays and their memory organization and further about 2D arrays and multidimensional arrays. In case you missed it, read them first. Coming to this tutorial, we’ll be discussing the way to develop our own data types in the C programming language. It is done through something known as structures, and that’s what we’ll explore about here.

Structure and User Defined Data Types in C Programming

Structure and User Defined Data Types in C Programming

What is a structure in C ?

Structure is basically a derived data type in C programming language. It is defined as a collection of variables (which may be of different data types) to be grouped together and referred to as a single entity. This gives you ability to define your own data types i.e. user defined data types.

Difference between a structure and an array?

The next question which may arise, would be what’s the difference between array and structure. Well, an array is homogeneous set of elements i.e. array is capable of holding different values but of same data type. On the other hand, structure is combination of variables of different data types.

Structures can be used to track information which is common to large group of entities.

How to declare a structure?

Let us first see the basic semantics of declaring a structure. It is much like defining ordinary variable like:

Now for structure, it is done through keyword struct like this:

Let us take an example to track record of students in a high school. Each student would have certain set of properties/attributes which would differentiate them from one another. We’ll consider a student has a roll number of type integer , name of type string and  marks of type float for the time being. Now, let’s define our structure named student with these properties.

Now that we have our basic structure with it’s data members ready, we can declare instances of it like:

Now, all members of the structure can be accessed via our instance named George using membership access or dot operator like:

and so on…

 

It’s essential to remember here that the members we declare in a structure would take the memory only when instances are declared. You’re free to name your instance anything. It’s just like declaring a variable whose data type would be now your own defined data type, which is struct student here.

Notice above how we’ve defined the instances of a structure, another way of declaring them can be like this:

So, for the above example, it can be done as follows:

Next, you can access each of the properties for a particular structure variable like :

Similarly all other structure variables’ data members can be defined.

 

That’s solve for this tutorial, a lot more is there to know about structure, which we’ll cover in the second part.

Bot who codes! A FOSS Contributor.

www.github.com/CuriousLearner

Leave a Reply

Your email address will not be published. Required fields are marked *