Structures in C Programming Language
Structure is a special type of C data type. Structures contains a number of data types grouped together. Structures in C is one of the excellent functionality provided in C. Structures makes C Programming language easy and simpler up to certain extent. Structure is most widely used in Programming.
Structures in C allows multiple data types to be grouped together. As a programmer I have used structures in C a lot and find this feature interesting too. Just go through this article and you too will find structures in C interesting.
Let us suppose you want to store basic information about animal such as age, name, gender and birth place. There are two approaches that can be followed.
1) Use one array each to store properties of animal as shown below.
int age[10]; char name[10][20] ; char gender[10]; char bPlace[10][20];
This approach is very tedious as number of characteristics related to animal might increase. Also, we need to keep the track of index of each and every variable. Hence, it becomes very difficult to handle such C programs.
2) Second approach is using structures in C.
At the end of this article you will understand the ease of using structures and its importance.
Declaring a Structure in C:
Structure in C is declared using the keyword ‘struct’.
Syntax:
struct struct-name { Element 1; Element 2; Element 3; . . . Element n; };
Please don’t forget to include semicolon (;) at the end of structure declaration as shown above.
For example, we can declare structure as follows:
struct animal { int age; char *name; char gender; char *bPlace; };
Here,
animal is the name given to structure.
age, name, gender and bPlace are the elements of the structure animal. We can call it as properties of the structure animal.
Declaring Structure variables:
Once the new structure data type has been defined, we can declare one or more variables of that type as shown below:
struct animal a1, a2;
This statement set aside space in memory.
Following are the ways in which we can declare structure variables.
1)
struct animal { int age; char *name; char gender; char *bPlace; }a1,a2;
2)
struct animal { int age; char *name; char gender; char *bPlace; }; struct animal a1, a2;
3)
struct animal { int age; char *name; char gender; char *bPlace; }; struct animal a1={12,”Sam”,’M’,”North America”}; struct animal a2={18,”Maxie”,’M’,”Amsterdam”};
Accessing structure Elements:
Now is the time to access structure elements after declaration of structure type and structure variables.
Syntax:
Consider,
struct animal { int age; char *name ; char gender; char *bPlace; }a1,a2;
Now, element ‘age’ for the variable ‘a1’ can be accessed using the statement,
a1.age;
Similarly, element ‘name’ for the variable ‘a1’ can be accessed using the statement,
a1.name;
Array of Structures:
We can create array of structure variables as follows:
struct animal { int age; char *name ; char gender; char *bPlace; }a[100];
The above declaration can store information of not more than 100 animals.
The values can be accessed as follows:
a[0].age =10;
The above statement will assign value 10 to the element ‘age’ of structure variable a[0].
Similarly,
We can assign values to the elements of other structure variables too.
a[1].age=12;
We can print the values as follows:
printf(“%d”,a[1].age);
The above statement will display 12.
Also See:
Program to demonstrate Structures in C Programming Language
what is the file concept in c..?
File Concept in C has not been explained here .
Sure… We are working on the same. Will publish the article on File concepts soon. Subscribe to our posts so that you are updated. Thanks.
sir,what is ment by data type,lenear and binary search .do u have data structure tutorial in online
y r u using * in name and bplace???
Strings in C are handled in 2 ways:
1. Char pointer
2. Character array
If we have used char array, we need to define the array size; this would cause problem if the size of string exceeds the array size. So, pointers are always preferred in this case,as they are pointing to the string address where length of string is not an issue.
i want briefly explanation about struture.
Respected sir or madam, plz give examples of programs where really structures are useful .Also plz tell about the input files and output files.
It is really helpful !
Thanks !
good one
* is nothing but a pointer to character here..ie.it is used to contain the address of a variable of character data type.We can use array of characters ..but with memory specification,pointer is more useful..
its a gud one..thnks 4 ur help
Waaao…. very worthy
Thank you everyone for the wonderful comments. I hope this website is helping every one out there.
If you like this website, kindly share it with everyone.
Thanks,
LearnCOnline Team
good .. . .
thank for it . . .
very gud one guys………..
sir u missed sme of the topics lik union and enum .can i get them??
thns for the helpful questions and answers
Nice
Write a program to find out the sum of two integers using structure….pls answer this question pls
oh it was very helpul to meh tnx a lot…
why to use semicolumn after struct ?
Hello,
That is the syntax that needs to be followed. It is defined that way. We can’t redefine the syntax. 🙂
Thanks,
LearnCOnline Team
To Learn C++ Online visit http://www.learncpponline.com
Thanks for the awesome guide. However it would be good if you include the file and typedef concept.
Would you be able to provide some guidance on putting structs as members of other structs (structs within structs)?
Including the concepts like adding the typedef, file will elaborate this guide, anyways good for beginners.
i want to ask you that why an empty structure allocates 1 byte of memory
Sir,
What is switch and break statements.I need full detail.How they work and how they are used
i want to know HISTORY of structure c programming language
Input-string
Output-srn tig
How this output we get?