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.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 73 responses to “C Tutorial – structures, unions, typedef”

Why not let us know what you think by adding your own comment!

  1. Ali on July 14th, 2013:

    what is difference when we define struct inside main function and outside main function.

  2. subramani on July 18th, 2013:

    Thanks and its very useful .
    I am beginner of the programming and understood myself is very easy..

  3. nisha on August 11th, 2013:

    good job…its easy to understand .thanks

  4. sabs on August 21st, 2013:

    Really very good…
    Content presentation is good… :) keep it up.

    put some real example too.

    thanks

  5. Rahul kumar on August 28th, 2013:

    This is very nice to learned c language….

  6. harshali dave on August 28th, 2013:

    gud one..
    nice explanation….
    nd dhe backgrond colour makes the text readable..
    nicely designed.. :-)

  7. Sam on September 14th, 2013:

    What is the actual mean of union? And for what purpuse its used by c?

  8. sathya on September 14th, 2013:

    simply superb

  9. sathish on September 17th, 2013:

    very good

  10. shubham on October 2nd, 2013:

    really very gud for read

  11. anjna on October 24th, 2013:

    really helpful site …..:)

  12. Tom on October 29th, 2013:

    Very helpful. Thanks!

  13. Neeraj Agrahari on October 30th, 2013:

    Thanks a lot. A beginner like me can easily understand what is the structure… thanks thanks thanks.

  14. bhaart on November 30th, 2013:

    Well done , its simple and easy to understand.
    thanks a lot !!

  15. Manikandan on December 5th, 2013:

    super…..example

  16. harshit on December 9th, 2013:

    nice solutions

  17. KIRUTHIKA on December 16th, 2013:

    good!!!!!! need much information

  18. swe on December 17th, 2013:

    Thnx a Lot..it cvrs almost most needed topics

  19. ALFA on January 6th, 2014:

    It’s so useful for me, Thnx a lot

  20. sil on January 27th, 2014:

    Nice tutorial… :) please keep posting more of such stuff….really easy to understand and revise :)

  21. guna on March 19th, 2014:

    #include

    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;
    }
    here y i dont use ptr_myindex as integer type .because it only stores address of the variable .y dont i use like this:-

    #include

    typedef struct telephone
    {
    char *name;
    int number;
    }TELEPHONE;

    int main()
    {
    TELEPHONE index;
    int *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;
    }

  22. CHITTI on April 3rd, 2014:

    how to get age using structures by taking two inputs(present date, date of birth)?

  23. Quang on May 7th, 2014:

    Great work! Thanks