C Tutorial – structures, unions, typedef
In the C language structures are used to group together different types of variables under the same name. For example you could create a structure “telephone”: which is made up of a string (that is used to hold the name of the person) and an integer (that is used to hold the telephone number).
Take a look at the example:
struct telephone
{
char *name;
int number;
};
Note: the ; behind the last curly bracket.
With the declaration of the structure you have created a new type, called telephone. Before you can use the type telephone you have to create a variable of the type telephone. Take a look at the following example:
#include<stdio.h>
struct telephone
{
char *name;
int number;
};
int main()
{
struct telephone index;
return 0;
}
Note: index is now a variable of the type telephone.
To access the members of the structure telephone, you must use a dot between the structure name and the variable name(variables:name or number.) Take a look at the next example:
#include<stdio.h>
struct telephone
{
char *name;
int number;
};
int main()
{
struct telephone index;
index.name = "Jane Doe";
index.number = 12345;
printf("Name: %s\n", index.name);
printf("Telephone number: %d\n", index.number);
return 0;
}
Type definitions and structures
Type definitions make it possible to create your own variable types. In the following example we will create a type definition called “intpointer” (a pointer to an integer):
#include<stdio.h>
typedef int *int_ptr;
int main()
{
int_ptr myvar;
return 0;
}
It is also possible to use type definitions with structures. The name of the type definition of a structure is usually in uppercase letters. Take a look at the example:
#include<stdio.h>
typedef struct telephone
{
char *name;
int number;
}TELEPHONE;
int main()
{
TELEPHONE index;
index.name = "Jane Doe";
index.number = 12345;
printf("Name: %s\n", index.name);
printf("Telephone number: %d\n", index.number);
return 0;
}
Note: The word struct is not needed before TELEPHONE index;
Pointer to structures
If you want a pointer to a structure you have to use the -> (infix operator) instead of a dot.
Take a look at the following example:
#include<stdio.h>
typedef struct telephone
{
char *name;
int number;
}TELEPHONE;
int main()
{
TELEPHONE index;
TELEPHONE *ptr_myindex;
ptr_myindex = &index;
ptr_myindex->name = "Jane Doe";
ptr_myindex->number = 12345;
printf("Name: %s\n", ptr_myindex->name);
printf("Telephone number: %d\n", ptr_myindex->number);
return 0;
}
Note: The -> (infix operator) is also used in the printf statement.
Unions
A union is like a structure in which all members are stored at the same address. Members of a union can only be accessed one at a time. The union data type was invented to prevent memory fragmentation. The union data type prevents fragmentation by creating a standard size for certain data. Just like with structures, the members of unions can be accessed with the . and -> operators. Take a look at the example:
#include<stdio.h>
typedef union myunion
{
double PI;
int B;
}MYUNION;
int main()
{
MYUNION numbers;
numbers.PI = 3.14;
numbers.B = 50;
return 0;
}
That’s all for this tutorial.
dis site saves a lot of time
thanks
really very very useful
i am very happy for this because it helps me to learn c language.
I worked in Java projects for 4 years after my college days.
Now i got a project in C.
It’s a real quick refresh after a long gap.
Thanks a lot
finally m clear that what is exactly typedef….
wow it is really awsm website to learn quickly…
It’s Great!
..
Thanks a lot!
It was really helpful!
thanx a lot…..its really very helpful n easy stuff
how can i make an array of structures?
#include<stdio.h>
struct point {
int x, y;
};
int main() {
point someArray[3] = {
{0,1},
{2,3},
};
printf(“%d %d\n”, someArray[0]);
printf(“%d %d\n”, someArray[1]);
return 0;
}
thankyou a lot
Very helpful and fast, exactly what i needed
Thanks and keep going!
Thanks a lot.
Very helpful and i got the knowledge….
Thanks again….
coolllll……………..
nice
super………….
very nice
How do you use structs within structs or unions?
precise and clear
u said members of a union can be set one at a time and here u hav done setting 2 values?????
excellent nice explanation
nice explanation
thanks
Good site, lot of info.
Thanks.
Eassy understable lang…once read … Gud to feell…and knw every thing about structure an union…..
This Site is good to understand and reduces time consumption a lot…
Thanks!
So useful!
i understood structures explanation,i just want Stack operation program,plz explain
superb..very effective..
this is an owesome site to improve our c language thanx for publishing it for free.
Thanx alot
this site is really awesome…logic given and explained in such a way that naive users can also understand…Lots of Thanks:)
very useful tutorial.
good site
Examples are very easy to understand….Thx
Awsm one…..
read it in 15 minutes…..save time..
thanks
Helpful site for Beginner,tnx.
its a very awosome i cleared my structure topic
This is really to the point and clears all points regrad strutures …….
Thanks a lot
carry on this work……..
it is very good ,,my structures is clear know ,,it is best for learning……
cool…………..
this site is vry useful n its language is vry easy n understandable…