0% found this document useful (0 votes)
9 views4 pages

Structures and Unions in C Lan

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

Structures and Unions in C Lan

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

C – Structure

1.Write the differences between structure and union.


2.Represent a complex number using a structure in C. Write a C program that uses functions to perform the following operations:(i)
Addition of two complex numbers (ii) Subtraction of two complex numbers
3.How to pass structure variable to functions? Explain with example.
4.How does a structure differ from an array?
5.What are the advantages and disadvantages with bit-fields?
6.Explain the following with examples: (i) self referential structures (ii) typedef (iii) command line arguments
7.How do you define a structure, structure variables, access their elements and perform operations on them? Explain with examples.
8.Explain the concept of Nested structures with a sample C program.
9What is structure? Explain the C syntax of structure declaration.
10 Define Structure? Explain how to declare a structure, accessing structure members with an example program.
11.Explain about declaration ,initialization and accessing of structures .And also discuss about complex structures.
12.Create a structure that displays student details and explain.
13.What are the advantages with bit fields?
14.Compare the features of the array with the structure data type in C language.
15.How structure is different form anarray? Explain with program.
16.Write a program to show the subject registration for an exam by the student using structures.
17Develop a C program to declare a structure with the following elements and for accessing them. 1.Name. 2.Age.
3.University.
18.Develop a program using pointers to structure to find the aggregate marks student_marks where the structure
consists of six subject marks.
19.Define self-referential structure.
20.Compare structures and unions.
21.List the difference bet ween structures and unions .Explain with an example.
22.Define Union? How to represent a union?
23.Develop a program that creates a union named customer to read and display customer attributes: cust_id(numeric),
age(numeric), account_no(string), andAddress(string).
24.Define the union and write a program to declare and access the union members. Consider employee details as an
example.
C Structure is a collection of different data types which are grouped together and each element in a C structure is called member.
Structure is also called heterogeneous collection of elements
• If you want to access structure members in C, structure variable should be declared.
• Many structure variables can be declared for same structure and memory will be allocated for each separately.
• It is a best practice to initialize a structure to null while declaring, if we don’t assign any values to structure members.
DIFFERENCE BETWEEN C VARIABLE, C ARRAY AND C STRUCTURE:
• A normal C variable can hold only one data of one data type at a time.
• An array can hold group of data of same data type(homogeneous).
• A structure can hold group of data of different data types
• Data types can be int, char, float, double and long double etc.

Type Using normal variable Using pointer variabe

struct tag_name struct tag_name


{ {
data type var_name1; data type var_name1;
data type var_name2; data type var_name2;
data type var_name3; data type var_name3;
Syntax }; };

struct student struct student


{ {
int mark; int mark;
char name[10]; char name[10];
float average; float average;
Example }; };

Declaring
structure variable struct student report; struct student *report, rep;

Initializing struct student rep = {100, “Mani”, 99.5};


structure variable struct student report = {100, “Mani”, 99.5}; report = &rep;

report.mark report -> mark


Accessing report.name report -> name
structure members report.average report -> average

USES OF C STRUCTURES:
1. C Structures can be used to store huge data. Structures act as a database.
2. C Structures can be used to send data to the printer.
3. C Structures can interact with keyboard and mouse to store the data.
4. C Structures can be used in drawing and floppy formatting.
5. C Structures can be used to clear output screen contents.
6. C Structures can be used to check computer’s memory size etc.
C – Array of Structures
As you know, C Structure is collection of different datatypes ( variables ) which are grouped together. Whereas, array of structures is
nothing but collection of structures. This is also called structure array in C.
Syntax : Struct structname objname[size];
Example : struct student a[10];
C – Passing struct to function
• A structure can be passed to any function from main function or from any sub function.
• Structure definition will be available within the function only.
• It won’t be available to other functions unless it is passed to those functions by value or by address(reference).
• Else, we have to declare structure variable as global variable. That means, structure variable should be declared outside the main
function. So, this structure will be visible to all the functions in a C program.
SYNTAX : FUNCTIONNAME(OBJ);

C – Structure using Pointer C structure can be accessed in 2 ways in a C program. They are,
1. Using normal structure variable 2. Using pointer variable
Dot(.) operator is used to access the data using normal structure variable and arrow (->) is used to access the data using pointer
variable.
Self referential structure: any Structure contains same Structure pointer variable
Struct student
{
Int sno;
Char sna[40],dept[10];
Struct student *p;
};
C – Nested Structure : Nested structure in C is nothing but structure within structure. One structure can be declared inside other
structure as we declare structure members inside a structure. The structure variables can be a normal structure variable or a pointer
variable to access the data.
Syntax: Struct structurename
{
Datatype var1,va2,..;
Struct structurename obj1,obj2…;
}

C – Typedef : Typedef is a keyword that is used to give a new symbolic name for the existing name in a C program. This is same like
defining alias for the commands.
Syntax : typedef datatype aliasname ;
example: typedef int number;
typedef struct student stud;
When we use “typedef” keyword before struct <tag_name> like above, after that we can simply use type definition “stud” in the C
program to declare structure variable.

Example program accept students data and print using structures and pointers
#include<stdio.h>
#include<stdlib.h>
struct student
{
Int rno;
Char na[40],d[10];
Long int fee;
};

void main()
{
struct student *a;
a=(struct student *) malloc (sizeof(struct student));
printf("\n\t Enter Student Roll Number : ");
scanf("%d",&a->rno);
printf("\t Enter Student name : ");
scanf("%s",a->na);
printf("\t Enter Student dept : ");
scanf("%s",a->d);
printf("\t Enter Student Fee : ");
scanf("%ld",&a->fee);
printf("\n\t Student Information is ");
printf("\n\t %d \t %s \t %s \t %ld",a->rno,a->na,a->d,a->fee);
}
C – Union C Union is also like structure, i.e. collection of different data types which are grouped together. Each element in a union is
called member.
• Union and structure in C are same in concepts, except allocating memory for their members.
• 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.
• Many union variables can be created in a program and memory will be allocated for each union variable separately.
• Below table will help you how to form a C union, declare a union, initializing and accessing the members of the union.
Type Using normal variable Using pointer variable

union tag_name union tag_name


{ {
data type var_name1; data type var_name1;
data type var_name2; data type var_name2;
data type var_name3; data type var_name3;
Syntax }; };

union student union student


{ {
int mark; int mark;
char name[10]; char name[10];
float average; float average;
Example }; };

Declaring
union variable union student report; union student *report, rep;

Initializing union student rep = {100, “Mani”, 99.5};


union variable union student report = {100, “Mani”, 99.5}; report = &rep;

report.mark report -> mark


Accessing report.name report -> name
union members report.average report -> average
DIFFERENCE BETWEEN STRUCTURE AND UNION IN C:
C Structure C Union

Union allocates one common storage space for all its


members.
Structure allocates storage space for all its Union finds that which of its member needs high storage
members separately. space over other members and allocates that much space

Structure occupies higher memory space. Union occupies lower memory space over structure.

We can access all members of structure at a time. We can access only one member of union at a time.

Structure example: Union example:


struct student union student
{ {
int mark; int mark;
char name[6]; char name[6];
double average; double average;
}; };

For above structure, memory allocation will be like


below.
int mark – 2B For above union, only 8 bytes of memory will be allocated
char name[6] – 6B since double data type will occupy maximum space of
double average – 8B memory over other data types.
Total memory allocation = 2+6+8 = 16 Bytes Total memory allocation = 8 Bytes

You might also like