Introduction to Newbie Programmer Series Introduction to Computer The Computer Programming Language Setting up the Computer for Programming
 Lets Get Started !
Beginning C Programming Variables a Handbag ! Basic Variable types in C Basic Formatted Output Basic Formatted Input
Looping Repeating Concept Different Kinds of Loops Advanced concepts and trick with Loops
Operators, An Introduction Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Miscellaneous Operators and Operators Precedence
 Controlling the Flow
Flow Charts Conditional Statements Using Goto and Label Break and Continue the loops Switch the Cases Some Practical Applications of Control Flow
 The program structure
The program Structure Functions More Concepts of Functions Scope of the Variables across functions How the C language itself Works ? Scope of the Variables across files Static Variables Auto and Register Variables #define PreProcessor Some more PreProcessors
 Arrays and Structures
Introduction to Arrays Arrays, some more Concepts Arrays, Cool Examples Structures Introduction Structure, Some more Concepts Structure, Some Examples
 File Management
Basics of File management in C Steps Involved with File Handling in C
Inside Logic Gates – The Electronic Logic Binary Number System Mathematical Reasoning
Newbie Programmer
full book coming soon
by Shubham Ramdeo

That's how we play with structs!

structureWelcome to the Newbie Programmer Series. In the last part, we have discussed structures and got a good introduction about them. We learned how to make them and use them as a real world object. In this part, we will discuss more advanced concepts about structure like using them with arrays and functions etc. So if you are new to this series, please go to index (click here) and check out all the previous parts so that you can easily understand what we are discussing down below.

Arrays of Structure

Let's get back to where we ended in the last part.

typedef struct person {
 char name[50];
 int age;
 float income;
 int child;
 person john, max;
 john.income = 40000;
 max.income = 2 * john.income;

In the above 'person' structure, we have used arrays inside it. The 'name' string. We can say :

person john;
john.name = "John The Great";

Now what would be 'john.name[3]' ? It's 'n'. So we can do anything with that. You can create any data table inside the structure. All the topics that we have a discussed with arrays can be easily implemented inside structures too. Just use '.' (Dot) to access them.

We can also make arrays of structure. For example, in the above code, we have created two persons. John and Max. What if we want to create 100 persons ? Like 100 employees in a company? We can make an array of 100 persons. Like this :

person employee[100];

Now Each of these 100 employees have the properties of person. If we choose that our Mr. John has a code number 44 then we can redefine his properties as :

employee[43].name = "John";
employee[43].age = 32;
employee[43].income = 40000;

It means that employee number 44 has name "John", age 32 and income 40000.

Nested Structure

We can also use structure inside structure, or what we should call now as " nested structure ". For example :

struct {
int age;
int income;
struct {
int price;
int weight;
} car;
int birthyear;
} john;

In this structure above, we have created another structure called car. Now we can do stuff like this :

john.age = 45;
john.car.price = 100000;

You can also make these structures separately and then combine when needed like :

typedef struct {
int price;
int weight;
} vehicle;

typedef struct {
int age;
int income;
vehicle car;
int birthyear;
} person;

person john;

john.car.price = 100000;

Same result. But the code looks more realistic.

I think I have used typedef too much. Please take a look the last post again to get it right!

A little geometry

My favorite application about structure is how they can be use beautifully with geometry. For example, how you represent a point on a graph paper? Such as in this picture below, the Red Point?

[caption id="attachment_476" align="aligncenter" width="150"]point at (2,3) See the red point located at (2 ,3)[/caption]

In this picture above, the horizontal line, on which 'X' is written, is called X axis, and the vertical line, on which 'Y' is written is called Y axis. They are scaled with numbers, negative to positive. Whatever point we want to locate, check out it's interaction with the X and Y axis and write it as (X , Y). So here, location of the point is (2, 3).

How can we represent such a point in C language? By using structs! And we will use typedef to use them as real objects. I mean as a variable type.

typedef struct {
int x;
int y;
} point;

Now see the magic :

point p1, p2;

p1.x = 2;
p1.y = 3;

p2.x = 5;
p2.y = 7;

Here we have created two points, p1(2,3) and p2(5,7).

Structure with Macros

You see we have assigned each value individually, (p1.x=2...). A better idea is to use a MACRO. Yes we can use structs with MACRO too. Such as :

#define ADDPOINT(NAME, X, Y) point NAME; NAME.x=X; NAME.y=Y;

Now we can simply do like this :

ADDPOINT (P, 2, 3);

This will create our point P with (2, 3) coordinates.

Structure with Functions

Just as we have used structure with MACRO, we can use them with functions too. Let take this coordinate geometry math further and learn one more thing. Using structure with functions. Declaring the point structure outside the main function, (making it global, see functions) we can make a function like :

struct point {
int x;
int y;
sayPoint(struct point p)
printf("The point has X axis %d and Y axis %d\n", p.x, p.y);

Now from the main function :

struct point A;

Will produce the output :

The point has X axis 2 and Y axis 3

I think it's enough for today. In the next post, we will discuss some more practical applications and will end the structure topic. So stay connected :)

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.

© Shubham Ramdeo

Site Map
© Shubham Ramdeo