Structures and Unions in C

nelavalli satheesh
3 min readMay 27, 2021

Structures and Unions in C, A structure is a collection of one or more variables, possibly of different data types, grouped together under a single name to represent a single entity.

Declaration:

struct name {
data_type mem1;
data_type mem2;
data_type mem3;
"
"
};
struct student {
int rollnum;
char name[256];
char branch[256];
char dept[256];
};
struct student var;

Initialization:

struct Tag_name object = {init values}

struct student var = {1, “sathi”, “ECE”, “ECE”};

Structure declarations will allow programmers to create new data types and will not be allocated with any space in memory. Structure declarations compiler will find the compositions of structure.

Size of structure = Sum of the bytes occupied by all the members + padded bytes if any (optional).

struct student size = sizeof(int) + sizeof(name) + sizeof(branch) + sizeof(dept) + padded bytes if any.

All structure members can be accesses at any time.

Data Alignment:

A data number is called naturally aligned if its address is aligned to its size (address is multiple of its size).

Structure Padding:

Padding is done by the compiler by pushing unused bytes in between the members of the structure to make sure that every member is aligned in the memory.

#include<stdio.h>
struct s {
int a;
int b;
};
void fun(struct s *x ,struct s *y,struct s *sum)
{
sum->a = x->a + y->a;
sum->b = x->b + y->b;
}
int main()
{
struct s obj1 ={10,10.8}, obj2 = {20, .2}, obj3;
fun(&obj1, &obj2, &obj3);
printf("size of %ld\n", sizeof(struct x));
printf("%d %d", obj3.a,obj3.b);
return 0;
}
Output:
size of 8
30 10

Structure bit fields:

Declare a structure data member with explicit size, in bits. Formally syntax is

struct name {

data_type member : width;

}

If you see the below example, struct x is a declaration without bit fields and struct y is a declaration with bit fields. This struct x allocated 8 bytes of memory space, but we are going to store either 0 or 1 in each of the variables.

This struct y allocated 4 bytes of memory space, but only 2 bits will be used store the values.

Example:

#include <stdio.h>
#include <string.h>
struct x{
int w;
int h;
};
struct y{
int w : 1;
int h : 1;
};
int main( ) {
printf( "size of struct x : %ld\n", sizeof(struct x));
printf( "size of struct y : %ld\n", sizeof(struct y));
return 0;
}
Output:
size of struct x : 8
size of struct y : 4

Union:

Structures and Unions in C, A union is a collection of one or more variables, possibly of different data types, grouped together under a single name to represent a single entity.

union size is equal to the largest member size. union only one member accessed at a time.

union name {
data_type mem1;
data_type mem2;
data_type mem3;
"
"
};
union student {
int rollnum;
char name[256];
};
struct union var;
#include<stdio.h>
#include<string.h>
union x {
int a;
char name[256];
};
int main()
{
union x obj;
printf("sizeof union %ld\n", sizeof(union x));
strcpy(obj.name,"satheesh");
printf("%s \n", obj.name);
return 0;
}
Output:
sizeof union 256
satheesh

For more information click here

--

--