This tutorial explains Structures in C Programming with examples.
Structures are one of the most important data types in C. They allow you to store data in an organized way, and they provide an easy way to access that data.
How to create a structure in C?
There are two ways to create a structure in C.
- By Using struct Keyword
- By Using typedef
Struct Keyword
To create a structure in C, you first need to define a structure template. This is done using the struct keyword, followed by the name of the structure and the variables that it will contain within curly braces:
struct Student { char name[50]; int id; float gpa; };
Next step is to create variables.
struct Student s1; struct Student s2;
You can access the individual members of a structure using the dot operator:
s1.name // accesses the name member of s1 s1.id // accesses the id member of s1 s2.gpa // accesses the gpa member of s2
You can also use the arrow operator -> to access members of structures if you have a pointer to the structure:
struct Student *sPtr; // assume sPtr points to some student... (*sPtr).name // same as sPtr->name - accessing name through pointer
typedef Keyword
We can also create structures by using typedef:
typedef struct { int x; int y; } myStruct;
This creates a new type called myStruct, which is a structure with two members, x and y. You can then create variables of this type and access their members in the same way as before.
Example:
In order to declare and initialize a variable of some structure, say struct A, we need to declare structure template first. Structure template plays the same role as type ‘int’, ‘char’ or other types in C language. For example,
typedef struct A { char a; int b; float c; }sa; /* sa is a new type for the above template */
Template tells the compiler how to allocate storage to its members and makes computer not to store any space to them. List of elements in the template specifies its members. Now we declare variable and initialize them. For example,
/* struct_dec.c -- typedef allows to create new type */ #include <stdio.h> /* structure declaration */ typedef struct A { /* typedef creates new type */ char surname[15]; int roll_no; char initial[5]; float marks; }New; int main(void) { New x = {"smith", 34, "J", 67.38}; /* x is a variable of New */ New y[10]; /* 'y' an array of 10 New elements */ New *pstruct = &x; /* 'pstruct' is a pointer to 'x' */ return 0; }
Notice that a structure variable is declared like variable of any other type, for example,
int a; char b; float c; int marks[100];
and it’s initialized by writing up member’s values, in order specified in template, and enclosed in a pair of curly braces. For example, in above program,
New x = {"smith", 34, "J", 67.38}; /* 'x' is a variable of New */
Like we do in case of other types as, for example,
int x; x = 10; /* x is assigned 10 */
Can we do the same with structure variables also? Won’t you like to try this out?
For example,
int main(void) { New x; /* x is a variable of New */ x = {"smith", 34, "J", 67.38}; /* If this WORKS or FAILS? */ return 0; }
So, what you noticed? Did this work fine? Or failed? Can you point out the reason of failure?
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for C Internship
- Practice Computer Science MCQs
- Check Computer Science Books
- Check C Books
- Apply for Computer Science Internship