Tag Archives: typedef

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 “Mobile”: 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 Mobile number).

Take a look at the example:


struct mobile
{
char *name;
int number;
};

Note: Put the ; behind the last curly bracket.

With the declaration of the structure you have created a new type, called mobile. Before you can use the type mobile you have to create a variable of the typemobile. Take a look at the following example:

       #include<stdio.h>

	struct mobile
	{
		char *name;
		int number;
	};

	int main()
	{
	   struct mobile contact;
	   return 0;
        }

Note:

contact is now a variable of the type mobile .

To access the members of the structure mobile , 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 mobile 
	{
		char *name;
		int number;
	};

	int main()
	{
		struct mobile contact;

		contact.name = "Amar sg";
		contact.number = 997861;
		printf("Name: %s\n", contact.name);
		printf("Mobile number: %d\n", contact.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 mobile 
	{
		char *name;
		int number;
	}MOBILE;

	int main()
	{
		MOBILE contact;

		contact.name = "Amar sg";
		contact.number = 997861;
		printf("Name: %s\n", contact.name);
		printf("Telephone number: %d\n", contact.number);

		return 0;
	}

 

Note: The word struct is not needed before MOBILE contact;

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 mobile
	{
		char *name;
		int number;
	}MOBILE;

	int main()
	{
		MOBILE  contact;
		MOBILE *ptr_mycontact;

		ptr_mycontact = &contact;

		ptr_mycontact->name = "Amar sg";
		ptr_mycontact->number = 997861;

		printf("Name: %s\n", ptr_mycontact->name);
		printf("Mobile number: %d\n", ptr_mycontact->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 mobileunion
	{
		double PI;
		int B;
	}MOBILEUNION;

	int main()
	{
		MOBILEUNION numbers;
		numbers.PI = 3.14;
		numbers.B = 50;

	    return 0;
	}

That’s all for this tutorial. Happy Programming :-)
Skip to toolbar