Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

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

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:

<data_type> <variable_name> // For variable

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

struct <struct_name>
{
  ...
};

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.

struct student
{
  int rollno;
  char name[20];  // Taking an array of character (string)
  float marks;
  // You are free to declare as many members as you want, but we're restricting our example to 3 data members (properties)
  // Size of the structure should be known to the compiler
};

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

struct student George;

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

George.rollno = 91;

George.marks = 78.6;

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:

struct <struct-name>
{
  // Data Members

} <name-of-first-structure-variable> ... ;

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

struct student
{
  int rollno;
  char name[20];  // Taking an array of character (string)
  float marks;
}George,Henry,Steve; // Declare as many as you want.

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

George.rollno = 50;
George.marks = 98.6;
strycpy(George.name, "George");

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.

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy