• Techiax

Tutorial - Structures for C++

Structures for C++:


Structures are basically a user defined data types where you can keep different data types (char, int, float, etc) into a single place. Arrays are also similar but they cannot store different datatypes, they could be used to store only similar data. Structures are useful for storing and accessing various types of information in a single file.

Also Read: Disable unwanted Windows startup programs to fasten your system

Defining a Structure:

To define a struct, you should use the keyword "struct". Don't forget to end it with semi-colon(;)

 struct structure_name
 {
 datatype1 variable_name;
 datatype2 variable_name;
 .........
 };

Example:

 struct person
 {
 int age;
 char name[50];
 float height;
 };



Referencing and accessing a Structure:

To use the structure, you can either give it a variable name at the end of struct definition before semicolon to make it global or you could reference it in your class or function with structure_name.
I will demonstrate both ways.
Then you can access the variables (members) you declared in the structure by using dot operator(.)

Example


 #include<stdio.h>
 #include<conio.h>
 #include<string.h>
 struct person
 {
 int age;
 char name[50];
 float height;
 }person1;         //person1 can be accessed globally
 void main()
 {
 struct person person2;    //struct keyword is optional, person2 can only be accessed locally.
 person1.age=25;
 strcpy(person1.name,"abc");  //strcpy is used to copy the strings.
 person1.height=5.4;
 person2.age=30;
 strcpy(person2.name,"xyz");
 person2.height=5.5;
 //display all the data
 //you can also use cout in C++
 printf("Person1 age=%d\n",person1.age);
 printf("Person1 name=%s\n",person1.name);
 printf("Person1 height=%f\n",person1.height);
 printf("Person2 age=%d\n",person2.age);
 printf("Person2 name=%s\n",person2.name);
 printf("Person2 height=%f\n",person2.height);
 getch();
 }

 Output:
 Person1 age=25
 Person1 name=abc
 Person1 height=5.4000
 Person2 age=30
 Person2 name=xyz
 Person2 height=5.5000

Also Read: Use your android phone as your computer's mouse and keyboard


Array of Structures:

An array of Structures is a type of collection where each element of the Array will reference to a separate structure. It is just like an array declaration and accessing.

Example:

 struct person
 {
 int age;
 char name[50];
 float height;
 } p1[10];

Another type of reference
 struct person p1[10];

Each element in the array (0,1,2,3,..9) will reference a different structure

To access elements of Array of structures

p1[0].age represents age of p1[0] structure.
p1[1].age represents age of p1[1] structure.

And other functions are similar like normal structure.

Also Read: Tutorial - Android Activity Lifecycle

Pointers for Structures:

Pointers for Structures is to reference an entire structure with use of pointers. Pointers reduce the use of memory. Pointers are important to implement data structures like linked list, queue, etc. We would post it in the upcoming posts.

 struct structure_name *pointer_name;
 pointer_name=&structure_variable;


To access structure elements using pointers you need to use -> operator instead of dot operator.

Example:


 #include<stdio.h>
 #include<conio.h>
 #include<string.h>
 struct person
 {
 int age;
 char name[50];
 float height;
 }person1;         //person1 can be accessed globally
 void main()
 {

 struct person *person_point;  //declare a pointer for struture
 person_point=&person1;   //reference the pointer person1 variable

 person_point->age=25;
 strcpy(person_point->name,"abc");  //strcpy is used to copy the strings.
 person_point->height=5.4;

 //you can also use cout in C++
 printf("Person1 age=%d\n",person_point->age);
 printf("Person1 name=%s\n",person_point->name);
 printf("Person1 height=%f\n",person_point->height);
 getch();
 }

 Output:
 Person1 age=25
 Person1 name=abc
 Person1 height=5.4000




Now you would have a good conceptual knowledge of structures. But don't stop with it. Try to use it in your programs with different ways each time and tell us of your achievements.


Also Read: Tutorial - C programming - Decision Statements


Share your views and thoughts . That will help us do more.






Author:

I am an enthusiastic person striving to gather as much information as possible to achieve higher heights and to share the information to all others to make them also reach the stars.
"People who are crazy enough to think they can change the world are the ones who do"
-Steve Jobs.

Previous Post
Next Post