fuddlepixelFuddlepixel coz tech is fun();

Structure Introduction

Let us discuss what are structure and how to use them in C language. How we can treat variables as a real world object using the struct.

By Shubham Ramdeo
19 April 2016 · 5 mins read
# c  newbie 

Structure Introduction

That’s how we can create structures of different types of data ! Like real world objects !

Welcome to the Newbie Programmer Series. With the last part [click here], we have completed arrays. Here we will start with a new and interesting topic called Structures. Why there is a need to use them and how to use them. So if you are new to this series, please go to the index [click here] and read out all the previous parts so that you can easily understand what is happening down below.

We have discussed arrays. That is how same type of many variables get packed into a single big variable like a table. For example, for some employee salary program, when we say

float person[10];

We make an array, called ‘person’ that can store 10 ‘float’ values.

We can do:

person[2]=4000.50;

This can be interpreted as the person number 3 has a salary of 4000.50$. (remember? arrays starts from zero?)

But that is not the complete solution. A ‘person’ is not limited by his ‘number’ and ‘salary’. A person has his age, name, grade, family members and many other properties. These all are different kind of properties. And all needs to be managed individually. Also, all these different properties of a person are if different types.

A salary is a float type of variable, age and family members are integer, his name is a string and grade is a character.

To make such a package with arrays is not possible. That’s why C language has a feature called ‘structure’.

We want to make ‘person’ variable a complete structure of different kinds of properties and variables.

So to make it a structure, we will do:

struct {
 //properties
} struct_name;

So the above example of person can be created as :

struct {
 int age;
 int familymembers;
 float salary;
 char grade;
 char[50] name;
} person;

Now we have created person as a structure.

To use a structure, use dot ( . ) as :

struct_name.property = value

So in the above example, to set the age of this person :

person.age = 32;

It’s simply saying “age of person is 32”. This is so easy to understand.

To print its value, you can use same dot :

printf(" Age is %d \n", person.age);

You are not limited to a single structure. You can do something like this to create more :

struct {
 int pages;
 float cost;
} book1, book2, book3;

This will create 3 structure variables. book1, book2, book3. And each of these can be used individually having same kind of properties. Like you can do :

book1.pages = 100;
book2.pages = 256;
book3.cost = 7.99;

Let us make a complete program

#include <stdio.h>
main()
{
 struct {
 int pages;
 int SellingCost;
 }book1, book2;

 float costperpage1, costperpage2;

 printf("Enter number of pages of Book 1 : ");
 scanf("%d", &book1.pages);
 printf(" Enter selling cost of Book 1 : ");
 scanf("%f", &book1.SellingCost);
 printf("Enter number of pages of Book 2 : ");
 scanf("%d", &book2.pages);
 printf(" Enter selling cost of Book 2 : ");
 scanf("%f", &book2.SellingCost);

 costperpage1 = book1.SellingCost/book1.pages;
 costperpage2 = book2.SellingCost/book2.pages;

 if(costperpage1 == costperpage2)
 {
 printf("Both books have same cost per page \n");
 }
 else if(costperpage1 > costperpage2)
 {
 printf("Book 1 has more cost per page \n");
 }
 else if(costperpage2 > costperpage1)
 {
 printf("Book 2 has more cost per page \n");
 }
 else 
 {
 printf("There is some ERROR in DATA \n");
 }
 printf("Thanks for using...\n");
 printf("Press Enter to EXIT\n");
 getchar();
 return 0;
}

OUTPUT :

 Enter number of pages of Book 1 : 400
 Enter selling cost of Book 1 : 20
 Enter number of pages of Book 1 : 600
 Enter selling cost of Book 1 : 30
 Both books have same cost per page
 Thanks for using...
 Press Enter to EXIT

Structure as objects

Don’t you think that the above examples of ‘person’, ‘book’ are all practically an object in real life ? They all are a type of their own and have their own properties ? I mean, we have integer type of variable in mathematics and programming but why can we a ‘person’ type of variable too ? Yes we can. We can create our own types of variable with C. We are not limited to ‘int’, ‘float’ etc only, we can create our own types. This is the basis of Object Oriented Programming. Well C language does not support so direct approach to object oriented programming but we can have a simple introduction with structs.

We can use a keyword ‘typedef’, to define a new type. We can use this keyword with any data types but we will do those advanced stuffs in the second book, here let us play with structs only.

typedef struct name {
 properties
 };

So now to use book as an individual object, a real life data type,

typedef struct book {
 int pages;
 float SellingCost;
 }

And then to make any variable a book, we can do it directly, simply :

book NewbieProgrammer;

Just as writing int x; Makes ‘x’ a integer variable, the above statement will make ‘NewbieProgrammer’ a book.

Also now you can access its properties directly :

NewbieProgrammer.pages = 250;

Using this style, we can make more programming more practical.

For example :

typedef struct person {
 char name[50];
 int age;
 float income;
 int child;
 };

person john, max;
john.income = 40000;
max.income = 2 * john.income;

It clearly tells us that “max’s income is two times of john’s income”.

I think its enough for this part. We have discussed a brief introduction about structures of C. In the next part, we will do some more important concepts about structs so stay connected :)


Share this :



Update me weekly ✉

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

I'm in ♥


Or follow us
   


About author

Shubham Ramdeo

Shubham Ramdeo

Shubham Ramdeo is the founder of Fuddlepixel. He is a geek, a writer, involved with many technical and humanity projects.

Meet me!



Recent





Related

c 

newbie 

Top