Skip to content

Structures in C

What is a structure?

A structure in c/ c++ is a user-defined data type that enables us to merge data items of different types. It also helps us in constructing significant and complex data types. A structure in c is identical to an array, with the only distinction being that an array stores information of the same data types.

For example: If you were to store the information about all the books present in a library,the following characteristics listed below might be necessary to monitor.

  • Title of the book
  • Author
  • Year of publishing
  • Subject
  • Book ID

How to create a structure?

Struct keyword is used to define a structure. This keyword is used to define a new data type which consists of more than one data type.

syntax:

struct <structName>;

{

<type> <membername1>;

<type> <membername2>;

<type> <membername3>;

…….

};

Firstly, the struct keyword is used as per the above syntax and then a name is given to the structure although it’s not compulsory. Following this is the declaration of all the member variables within the curly braces such as int, float, an array or any such variable definition etc. One or more structure variables can be initialized after closing curly braces but again this is optional.

Note:

1. A semicolon must follow the closed curly brace in a structure type statement (;). 2. The struct keyword can be used before declaring a variable in C++ whereas it is required in C programming.

Following is an example of the struct statement:

Firstly in the above program, a structure has been declared by the struct student to store the student’s details such as name, roll no.,percent etc. These are known as structure elements.

Each member may have a data of different type, for example- name is an array of char type and roll no. is of int type. The name of the structure is student and is called structure tag.

Declaring Variables in Structures

Variables of the structure are declared either while a structure is being constructed or once it has been defined. The variable declaration of any structure variable is identical to the declaration of a simple variable of any other data types. Variable declaration of the structure takes place in following ways:

Separately declaring Structure variables

2) Declaration of the structure variable using structure definition.

Here, stud1 and stud2 are variables of structure admission.

Accessing structure members:

You can access Structure members and allocate them in a plethora of ways. Without the structure, the structure members have no purpose. If you want to allot a value to any member of the structure, then the member name must be connected to a structure variable using a dot in order to assign a value for structure members. Operator is often known as a period or a member access operator.

For example:

#include<stdio.h>

struct Point

{

int a, b;

};

int main()

{

struct Point p1 = {2, 3};

// Accessing members of point p1

p1.a = 20;

printf (“a = %d, b = %d”, p1.a, p1.b);

return 0;

}

Output: a= 20, b=3.

How to Initialize structure?

You can initialise structure members in any sequence due to designated Initialization. The C99 standard now supports this functionality. This feature only works in C programming.

Array of structure:

Arrays of structures can be created like other basic data types.

example:

#include<stdio.h>

struct Point

{

int a, b;

};

int main()

{

// Create an array of structures

struct Point arr[25];

// Access array members

arr[0].a = 30;

arr[0].b = 40;

printf(“%d %d”, arr[0].a, arr[0].b);

return 0;

}

Output: 30 40

What is a structure pointer?

We can have a structure pointer like primitive types. In order to access the members of the structure pointer, the arrow ( ->) operator should be used.

example:

#include<stdio.h>

struct Point

{

int a, b;

};

int main()

{

struct Point p1 = {7,8};

// p2 is a pointer to structure p1

struct Point *p2 = &p1;

// Accessing structure members using structure pointer

printf(“%d %d”, p2->a, p2->b);

return 0;

}

Output: 7 8

C Unions

A union is basically a type of data in C that allows you to store distinct data types in same memory location. Unions and structures are conceptually quite similar and are only different in terms of storage. Each member of a structure in C contains its own storage place, whereas all the union members have a single shared memory space equivalent to the size of its largest data member.

Defining a Union:

The definition of a union is the same to that of structure. The union statement specifies a new data type for your application which contains one or more members. The format of union declaration is shown below:

union [union tag] {

member definition;

member definition;

member definition;

} [one or more union variables];

The union tag is not mandatory and each member definition such as int i or float f is a standard variable definition.

Accessing a Union member in C

The syntax for gaining access to any union member is identical to that for gaining access to structure members.

syntax:

union test

{

int x;

float y;

char z;

}t;

t.x; //to access members of union t

t.y;

t.z;

Example:

#include <stdio.h>

union item

{

int x;

float y;

char ch;

};

int main( )

{

union item it;

it.x = 22;

it.y = 30.2;

it.ch = ‘z’;

printf(“%d\n”, it.x);

printf(“%f\n”, it.y);

printf(“%c\n”, it.ch);

return 0;

}

Output: 1106352506

30.199940

z

As we can see in above output obtained, the values of a and b get distorted and only variable type in c produces the anticipated result.This happens due to the fact that memory is shared across different data kinds in union. Consequently, memory will be assigned to the only member whose value is currently safe.

Drawbacks of C structures

Structure in C programming paves a way to store data of different types together. Structure is a user defined tool and is used to manage a collection of logically connected data elements but it contains the following drawbacks:

Static members:

Static members cannot exist within the body of the C structure.

Access modifiers:

Access modifiers are not supported in the C programming language. As a result, they cannot be utilised in C Structures.

No data hiding:

This user defined data type does not permit data hiding. We can access members of Structure type in C by any function, anywhere in the scope of a Structure.

Functions inside structure:

Structures in C are not allowed to include functions.

Construction creation in structure:

C structures can not have constructor inside structures.

nv-author-image

Vaibhav Kapoor

A professional web entrepreneur, WordPress developer and digital marketing strategist with more than 4 years of experience in building a business from scratch. Knowledgeable about SEO, blogging, internet marketing, social media and website development.

Leave a Reply

Your email address will not be published. Required fields are marked *

[wpfepp_submission_form form="1"]