0% found this document useful (0 votes)
209 views18 pages

Structures and Unions

This document discusses structures and unions in C/C++. It defines a structure as a user-defined data type that groups different data types together under a single name. Structures allocate separate memory for each member, while unions allocate shared memory for members. The document shows how to declare and initialize structures, access structure members, create arrays of structures, and use pointers to structures. It provides examples of defining, initializing, and accessing structures and unions in C code.

Uploaded by

Pruthvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
209 views18 pages

Structures and Unions

This document discusses structures and unions in C/C++. It defines a structure as a user-defined data type that groups different data types together under a single name. Structures allocate separate memory for each member, while unions allocate shared memory for members. The document shows how to declare and initialize structures, access structure members, create arrays of structures, and use pointers to structures. It provides examples of defining, initializing, and accessing structures and unions in C code.

Uploaded by

Pruthvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Nagarjuna College and Engineering

and Technology

Placement Training
on
Structures and Unions

By
Ashwini S S
Assistant Professor
Department of ISE
NCET
1
Structures
 A structure is a user defined data type in C/C++. A structure
creates a data type that can be used to group items of possibly
different types into a single type.
 It can have integer, float, double or character data in it.

struct structureName
{
dataType member1;
dataType member2;
...
}; 2
struct address
{
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
3
Create struct Variables
When a struct type is declared, no storage or memory is
allocated. To allocate memory of a given structure type and
work with it, we need to create variables.

struct Person
{
char name[50];
int id;
float salary;
};

int main()
{
struct Person person1, person2, p[20];
return 0;
}
4
Another way of creating a struct variable is:

struct Person
{
char name[50];
int citNo;
float salary;
} person1, person2, p[20];

In both cases, two variables person1, person2,


and an array variable p having 20 elements of
type struct Person are created.
5
How to initialize structure
members?
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize
members here
int y = 0; // COMPILER ERROR: cannot initialize
members here
};

The reason for above error is simple, when a


datatype is declared, no memory is allocated
for it. Memory is allocated only when
variables are created.
6
Structure members can be initialized using curly
braces ‘{}’. For example, following is a valid
initialization.
struct Point
{
int x, y;
};

int main()
{
struct Point p1 = {0, 1};
}
A valid initialization. member x gets value 0 and y
gets value 1. The order of declaration is followed. 7
How to access structure elements?
Members of the structure are accessed using dot(.)
operator.

struct Point
Output:
{
int x, y; x=20, y=1
};
int main()
{
struct Point p1 = {0, 1};
p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y);
return 0;
} 8
Keyword typedef
We use the typedef keyword to create an alias name
for data types. It is commonly used with structures
to simplify the syntax of declaring variables.

struct Distance{ typedef struct Distance{


int feet; int feet;
float inch; float inch;
}; } distances;

int main() { int main() {


struct Distance d1, d2; distances d1, d2;
} }
9
C Unions
• A union is a user-defined type similar to
structs in C programming
• We use the union keyword to define unions

union car
{
char name[50];
int price;
};
10
Difference between Structures and
Unions
#include <stdio.h>
union unionJob
{
char name[32];
float salary;
int workerNo;
} uJob;

struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
11
int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;
}

Output:

size of union =
size of structure =

12
• Structure allocates storage space for all its members
separately.
Whereas, Union allocates one common storage
space for all its members
• We can access only one member of union at a time.
We can’t access all member values at the same time
in union.
• But, structure can access all member values at the
same time.
This is because, Union allocates one common storage
space for all its members. Where as Structure
allocates storage space for all its members
separately.

13
Array of Structure
Structure is collection of different data type.
An object of structure represents a single
record in memory, if we want more than one
record of structure type, we have to create an
array of structure or object. As we know, an
array is a collection of similar type, therefore
an array can be of structure type.

14
#include<stdio.h>

struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};

void main()
{
int i;
struct Employee Emp[ 3 ];

15
for(i=0;i<3;i++)
{

printf("\nEnter details of %d Employee",i+1);

printf("\n\tEnter Employee Id : ");


scanf("%d",&Emp[i].Id);

printf("\n\tEnter Employee Name : ");


scanf("%s",&Emp[i].Name);

printf("\n\tEnter Employee Age : ");


scanf("%d",&Emp[i].Age);

printf("\n\tEnter Employee Salary : ");


scanf("%ld",&Emp[i].Salary);
}

printf("\nDetails of Employees");
for(i=0;i<3;i++)
printf("\n%d\t%s\t%d\t%ld",Emp[i].Id,Emp[i].Name,Emp[i].Age,Emp[i].Salary);

16
Structure using pointer

Like primitive types, we can have pointer to a


structure. If we have a pointer to structure,
members are accessed using arrow ( -> )
operator.

17
#include <stdio.h>
#include <string.h>

struct student
{
int id;
char name[30];
float percentage;
};

int main()
{
int i;
struct student record1 = {1, "Raju", 90.5};
struct student *ptr;
ptr = &record1;

printf("Records of STUDENT1: \n");


printf(" Id is: %d \n", ptr->id);
printf(" Name is: %s \n", ptr->name);
printf(" Percentage is: %f \n\n", ptr->percentage);
return 0;
}
18

You might also like