C Structure - Definition, Declaration, Access with/without pointer

Learn: What are the User defined data types in C language like structure, how structure is defined and how their members can be accessed through structure variable and pointer of structure variable.

This article will cover a brief on user defined data types in C. C supports various data types, out which there are few where programmer gets some benefit. User defined data types are used to create a variable which can contain few many other data types inside them. One variable containing many other variables, that's what we mean by user defined data types.

A user will have complete privilege to keep certain combinations of data types as per the need and call it a new variable itself.

What is structure in C?

We can start with structures in this article and following articles will continue on the other types. Structure uses a keyword struct. A structure can have a declaration like below

Declaration/Definition of a structure

struct tagname{ 
	char 	x;
	int  	y;
	float 	z;
};

Above is the definition for the structure, it says the compiler that what variables it will contain. Now we need to declare an object of that structure. Also in the above we can think of the tagname as data types names int , char, etc.

Declaration of a structure variable

Now use the tagname for getting an object, below is the syntax

struct tagname structvariable;

When we write the above piece of code, compiler would allocate contagious memory which can accommodate everything this struct has.

Accessing structure members through structure variable

Since we have created many variables inside the structure we may want to access them and do some operations, below are the few examples:

structvariable.x='A';	
/* this will write 'A' for the element x of structure structvariable*/

Similarly we can also access all other variables

structvaraible.y=20;
structvariable.z=10.20f;

Structure pointer - Declaration, Accessing of Structure members

The objects to a structure can also be a pointer same like we can create a pointer of int. To achieve this which we will need following declaration:

struct tagname *structPtrVariable;

To access the elements inside the structure we should be using the following syntax

structPtrVariable->x = 'A' // here '.' is replace by '->'
structPtrVariable->y = 20;
structPtrVariable->z = 10.20f;

Above we have created two objects for the struct tagname. Those two objects have independent memory allocated for each of them. Both of them can be compared with the normal declaration of the variables for example:

int a; 
int *a;

Structures play very important role in big systems where we need to combine several data's into a set and need to capture multiples of that set, perform operations and many more.

It even helps on the smaller systems.

A small piece of code will help understand the use of structures better.

#include <stdio.h>

struct tagname {
	char Char;
	int  Int;
	float Dec;
};

int main()
{
	struct tagname StructObj;
	struct tagname *ptrStructObj=&StructObj;

	StructObj.Char='H';
	ptrStructObj->Int=927;
	ptrStructObj->Dec=911.0f;

	printf("%C\n",StructObj.Char);
	printf("%d\n",ptrStructObj->Int);
	printf("%f",ptrStructObj->Dec);
	printf("\n");
	
	return 0;
}

Output

H
927
911.000000

Structures can be used like all other variables. Little practise will help.


Related Tutorials




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.