What is structure in c programming?

C structures are a derived data type.

structure in C allows to pack up more than one type of data in a single variable.

structure can be defined keyword struct.

dot (.) operator is used to access the structure member and arrow (->)operator is used to access the structure member for pointer to the structure.

Consider an instance that a car has different properties simply we can compact all the properties of that car to represnet it as a single entity. An example of the car we can see that the colour is of string type and the the number of seat is int the car model is of string type the manufacturer is string type all the properties can be combined and stored in a single variable that is structure.


How to define c structures

We can define a structure by using keyword struct then starting with flower bracket defining the data items of the structure then close the flower bracket then a struct variable followed by a semicolon (;) . Structure variable name is not mandatory it is optional.

Look at the example below

struct structure tag {

   member definition;
   .
   .
   .
}Structure_variable_name; 

The structure member refers to the variable.

Refer to the example program below.

#include<stdio.h>
int main(int argc, char const *argv[])
{
	struct my_struct
	{
		int variable1;
		int variable2;
	};
	struct my_struct example;
	example.variable1=100;
	example.variable2=20;
	printf("C Structures\nVariable 1 : %d\nVariable2 : %d",example.variable1,example.variable2);
	return 0;
}
c structures
c structures

Accessing the members of c structures

As we already know structure in C is a compaction of one or more variable. In order to access the member we used dot operator.

In technically we can say that structure variable followed by the dot operator which is then followed by the member name. In case of pointer to structure we use the arrow operator to access it instead of dot operator

A simple example to access structure variable is shown below

#include<stdio.h>
int main(int argc, char const *argv[])
{
	struct my_struct
	{
		int variable1;
		int variable2;
	};
	struct my_struct example;
	example.variable1=100;
	example.variable2=20;
	printf("C Structures\nVariable 1 : %d\nVariable2 : %d",example.variable1,example.variable2);
	return 0;
}
Accessing the members of c structures
Accessing the members of c structures

Pointer to c structures

A pointer to a structure can be created by using star (*) operator as shown in the example. In order to access the member for structure pointer use the arrow operator.

A simple example of accessing structure member through pointer is shown below

Step 1: create a pointer variable for structure.
struct car *pointer;

Step 2 : Assign the address of a structure variable by using & operator or allocate memory for structure variable dynamically.
*pointer=&car_structure;

Step 3: access the member of the structure variable through the pointer which is pointing at that variable using the arrow operator.
*pointer->name;

Try out a simple example program.

#include<stdio.h>
int main(int argc, char const *argv[])
{
	struct my_struct
	{
		int variable1;
		int variable2;
	};
	struct my_struct example;
	example.variable1=100;
	example.variable2=20;
	struct my_struct *ptr=&example;
	printf("C Structures\nVariable 1 : %d\nVariable2 : %d",ptr->variable1,ptr->variable2);
	return 0;
}
Pointer to c structures
Pointer to c structures

Function arguments as c structures

Structure variable can be passed to a function through parameters by specifying the structure name followed by a variable like passing a variable as a parameter.

Try out simple example shown below.

#include<stdio.h>
struct my_struct
{
	int variable1;
	int variable2;
};
void my_function( struct my_struct example )
{
	printf("C Structures\nVariable 1 : %d\nVariable2 : %d",example.variable1,example.variable2);
}
int main(int argc, char const *argv[])
{
	
	struct my_struct example;
	example.variable1=100;
	example.variable2=20;
	my_function(example);
	return 0;
}
Function arguments as c structures
Function arguments as c structures

Return structure from function

You can simply return structure variable using return statement.

You have to specify the return type of the function in the function prototype.

Try out simple program below.

#include<stdio.h>
struct my_struct
{
	int variable1;
	int variable2;
};
struct my_struct my_function( )
{
	struct my_struct example;
	example.variable1=100;
	example.variable2=20;
	return example;
}
	
int main(int argc, char const *argv[])
{
	
	
	struct my_struct example=my_function();
	printf("C Structures\nVariable 1 : %d\nVariable2 : %d",example.variable1,example.variable2);

	return 0;
}
Return structure from function
Return structure from function

Array of structures

sometimes it is handy to use an array of structures. Say for instance to store data about 100 students. C provides such a feature to use an array instead of creating 100 different structure variables.

Example

#include<stdio.h>
int main()
{
  struct student
   {
     char name[20];
     int marks;
     int rollnumber;
     int age;
   }s[100];

  printf("enter the students detail:name,marks,rollnumber,age\n");
  for(i=0;i<100;i++)
   scanf("%s%d%d%d",s[i].name,&s[i].marks,&s[i].rollnumber,&s[i].age);
  for(i=0;i<100;i++)
   printf("%s\t%d\t%d\t%d\n",s[i].name,s[i].marks,s[i].rollnumber,s[i].age);
  return 0;
}

Nested structures

One structure can be nested within another structure. In the below example to nest structures dot operator is used twice.

Example

#include<stdio.h>
int main()
{
  struct address
  {
   char phone[20];
   char city[25];
  };
  struct student
  {
   char name[30];
   int age;
   struct address a;
  };
  struct student e={"kriston",27,"8381234","newyork"};
  printf("name=%s\t age=%d \t phone=%d \t city=%s\n",e.name,e.age,e.a.phone,e.city);
  return 0;
}

People also ask for

What are the uses of structures?

The structures are useful in following ways
1.Hiding a file from directory
2.Checking the memory size of the computer
3.Changing the size of the cursor
4.Clearing the contents of the screen
5.Placing the cursor at an appropriate position on screen
6.Interacting with the mouse
7.Receiving a key from the keyboard
8.sending the output to printer


Summary

  • What is C structure?
  • How to declare C structure.
  • How to access structure member.
  • Pointer to C structure.
  • Pass structure to a function.
  • Return a structure from a function.

People also read