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. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

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

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

  1. Prithvi Macherla on May 25th, 2011:

    dis site saves a lot of time ;)

  2. sandhya on November 12th, 2011:

    thanks :)

  3. vikas on March 3rd, 2012:

    really very very useful

  4. prakash on April 25th, 2012:

    i am very happy for this because it helps me to learn c language.

  5. sk on May 18th, 2012:

    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 :)

  6. prakirtita on May 31st, 2012:

    finally m clear that what is exactly typedef….
    :)

  7. lalli on June 7th, 2012:

    wow it is really awsm website to learn quickly…

  8. vishaala on July 4th, 2012:

    It’s Great! :)..

  9. Maha on July 18th, 2012:

    Thanks a lot!
    It was really helpful!

  10. SHALINI on August 7th, 2012:

    thanx a lot…..its really very helpful n easy stuff

  11. eurich on August 12th, 2012:

    how can i make an array of structures?

  12. admin on August 12th, 2012:

    #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;
    }

  13. venkat509 on September 10th, 2012:

    thankyou a lot

  14. Georg on September 10th, 2012:

    Very helpful and fast, exactly what i needed
    Thanks and keep going!

  15. PUVANARAJAN on September 24th, 2012:

    Thanks a lot.

    Very helpful and i got the knowledge….
    Thanks again….

  16. subh on October 28th, 2012:

    coolllll……………..

  17. rani on October 31st, 2012:

    nice

  18. swathi on October 31st, 2012:

    super………….

  19. santho on November 21st, 2012:

    very nice

  20. YourSiteRocks on December 5th, 2012:

    How do you use structs within structs or unions?

  21. Shivam on December 7th, 2012:

    precise and clear

  22. shabeer on December 19th, 2012:

    u said members of a union can be set one at a time and here u hav done setting 2 values?????

  23. ramu on December 27th, 2012:

    excellent nice explanation

  24. sangita on January 1st, 2013:

    nice explanation

  25. talpa on January 7th, 2013:

    thanks

  26. Abhay on January 10th, 2013:

    Good site, lot of info.
    Thanks.

  27. Vikky sharma on January 22nd, 2013:

    Eassy understable lang…once read … Gud to feell…and knw every thing about structure an union…..

  28. Chandan Singh on January 25th, 2013:

    This Site is good to understand and reduces time consumption a lot…

  29. Sharif on February 4th, 2013:

    Thanks!
    So useful!

  30. sriveni on February 12th, 2013:

    i understood structures explanation,i just want Stack operation program,plz explain

  31. silpa,Melby on March 2nd, 2013:

    superb..very effective..

  32. kiran on March 5th, 2013:

    this is an owesome site to improve our c language thanx for publishing it for free.

  33. rajshree on March 13th, 2013:

    Thanx alot

  34. Sripooja on March 22nd, 2013:

    this site is really awesome…logic given and explained in such a way that naive users can also understand…Lots of Thanks:)

  35. rajkumar on April 7th, 2013:

    very useful tutorial.

  36. mashooque ghoto on April 10th, 2013:

    good site

  37. Narasimman on April 18th, 2013:

    Examples are very easy to understand….Thx

  38. rahul chhangani on April 22nd, 2013:

    Awsm one…..
    read it in 15 minutes…..save time..
    thanks

  39. sohag on April 28th, 2013:

    Helpful site for Beginner,tnx.

  40. jay pandya on May 8th, 2013:

    its a very awosome i cleared my structure topic

  41. Khet kumar on May 10th, 2013:

    This is really to the point and clears all points regrad strutures …….
    Thanks a lot
    carry on this work……..

  42. khalil baloch Buledi on May 14th, 2013:

    it is very good ,,my structures is clear know ,,it is best for learning……

  43. jayanthi on May 16th, 2013:

    cool…………..

  44. unnati on May 18th, 2013:

    this site is vry useful n its language is vry easy n understandable…

  45. sumaia akhter on June 21st, 2013:

    wht a site!!vry usefl..

  46. Sheik Arbaz on June 21st, 2013:

    Suprebbbbbbbbbbbb.site

  47. venkatesh on June 24th, 2013:

    Thanx a lot…good concept….it was really helpful for who are learning data structures…and easy to understand.

  48. keshav Kumar on July 6th, 2013:

    thank you……
    it’s vry useful 4 all who want to learn..

  49. Tiffany on July 8th, 2013:

    Great tutorial.
    Just one question why is it that there was an “*” before the name of the variable “name”? I thought that the “*” was used to say that the variable was a pointer which doesn’t seem to be the case here.

  50. admin on July 8th, 2013:

    @Tiffany – We have a structure with two variables, one of the type int and one char pointer called name. If you want to store one char, you would use char. If you want to store an array of character (in this case a name), you can use char pointer. (You point to an address in memory where the character array name is stored).
    Hope this helps!

  51. Ali on July 14th, 2013:

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

  52. subramani on July 18th, 2013:

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

  53. nisha on August 11th, 2013:

    good job…its easy to understand .thanks

  54. sabs on August 21st, 2013:

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

    put some real example too.

    thanks

  55. Rahul kumar on August 28th, 2013:

    This is very nice to learned c language….

  56. harshali dave on August 28th, 2013:

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

  57. Sam on September 14th, 2013:

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

  58. sathya on September 14th, 2013:

    simply superb

  59. sathish on September 17th, 2013:

    very good

  60. shubham on October 2nd, 2013:

    really very gud for read

  61. anjna on October 24th, 2013:

    really helpful site …..:)

  62. Tom on October 29th, 2013:

    Very helpful. Thanks!

  63. Neeraj Agrahari on October 30th, 2013:

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

  64. bhaart on November 30th, 2013:

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

  65. Manikandan on December 5th, 2013:

    super…..example

  66. harshit on December 9th, 2013:

    nice solutions

  67. KIRUTHIKA on December 16th, 2013:

    good!!!!!! need much information

  68. swe on December 17th, 2013:

    Thnx a Lot..it cvrs almost most needed topics

  69. ALFA on January 6th, 2014:

    It’s so useful for me, Thnx a lot

  70. sil on January 27th, 2014:

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

  71. 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;
    }

  72. CHITTI on April 3rd, 2014:

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

Leave a Reply: