0% found this document useful (0 votes)
176 views31 pages

4.unit - Iv

The document discusses structures in C programming. It defines a structure as a collection of variables of different data types grouped under a single name. There are two ways to declare structures - tagged structure and typedef structure. The document provides examples of declaring structures for student data and course data in a college. It also discusses initializing structures, accessing structure members using the dot operator, and provides a program example to initialize and display data of a personal structure.
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)
176 views31 pages

4.unit - Iv

The document discusses structures in C programming. It defines a structure as a collection of variables of different data types grouped under a single name. There are two ways to declare structures - tagged structure and typedef structure. The document provides examples of declaring structures for student data and course data in a college. It also discusses initializing structures, accessing structure members using the dot operator, and provides a program example to initialize and display data of a personal structure.
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/ 31

Q&A for Previous Year Questions Subject: CP (B.Tech.

I Year) Subject Code: CS204ES


UNIT-IV
---------------------------------------------------------------------------------------------------------------------------------------
---

UNIT–IV: Structure: Basics of Structures, Structures to Functions, Arrays of Structures,


Structures with in Structures, Arrays with in structures, Unions.

UNIT-IV

1. What is a structure? Write the syntax for structure declaration.


Ans:

Definition:

A structure is a collection of one or more variables of different data types, grouped together
under a single name. By using structures variables, arrays, pointers etc can be grouped together.

Structures can be declared using two methods as follows:

(i) Tagged Structure:

The structure definition associated with the structure name is referred as tagged structure. It does
not create an instance of a structure and does not allocate any memory.

The general form or syntax of tagged structure definition is as follows,

struct TAG Ex:- struct student

{ {

Type varaible1; int htno[10];

Type variable2; char name[20];

…… float marks[6];

…… };

Type variable-n;

};

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
Where,

- struct is the keyword which tells the compiler that a structure is being defined.
- Tag_name is the name of the structure.
- variable1, variable2 … are called members of the structure.
- The members are declared within curly braces.
- The closing brace must end with the semicolon.
(ii) Type-defined structures:-

- The structure definition associated with the keyword typedef is called type-defined structure.

- This is the most powerful way of defining the structure.

The syntax of typedefined structure is

typedef struct Ex:- typedef struct

{ {

Type varaible1; int htno[10];

Type variable2; char name[20];

…… float marks[6];

…… }student;

Type variable-n;

}Type;

where
- typedef is keyword added to the beginning of the definition.

- struct is the keyword which tells the compiler that a structure is being defined.
- variable1, variable2…are called fields of the structure.
- The closing brace must end with type definition name which in turn ends with semicolon.

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------

Variable declaration:

Memory is not reserved for the structure definition since no variables are associated with the
structure definition. The members of the structure do not occupy any memory until they are
associated with the structure variables.

After defining the structure, variables can be defined as follows:

For first method,

struct TAG v1,v2,v3….vn;

For second method, which is most powerful is,

Type v1,v2,v3,….vn;

Alternate way:

struct TAG

Type varaible1;

Type variable2;

……

……

Type variable-n;

} v1, v2, v3;

Ex:

struct book

{
char name[30];

int pages;

float price;

}b1,b2,b3;

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------

2. Declare the C structures for the following scenario:


(i) College contains the following fields: College code (2characters), College Name, year of
establishment, number of courses.

(ii) Each course is associated with course name (String), duration, number of students. (A
College can offer 1 to 50 such courses)

Ans:

(i). Structure definition for college :-

Struct college

char code[2];

char college_name[20];

int year;

int no_of_courses;

};

Variable declaration for structure college :-

void main( )

struct college col1,col2,col3;

….
}

(ii). Structure definition for course :-


struct course

char course_name[20];

float duration;

int no_of_students;
};

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------

Variable declaration for structure course :-

void main( )

struct course c1,c2,c3;

….

3. How to initialize structures in ‘C’? Write example.


Ans:
The rules for structure initialization are similar to the rules for array initialization. The initializers
are enclosed in braces and separated by commas. They must match their corresponding types in
the structure definition.

The syntax is shown below,

struct tag_name variable = {value1, value2,… value-n};

Structure initialization can be done in any one of the following ways

(i) Initialization along with Structure definition:-

Consider the structure definition for student with three fields name, roll number and average
marks. The initialization of variable can be done as shown below,

struct student

char name [5];


int roll_number;

float avg;

} s1= {“Ravi”, 10, 67.8};

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------

(ii) Initialization during Structure declaration:-

Consider the structure definition for student with three fields name, roll number and average
marks. The initialization of variable can be done as shown below,

typedef struct

int x;

int y;

float t;

char u;

} SAMPLE;

Figure Initializing Structures


The figure shows two examples of structure in sequence. The first example demonstrates what
happens when not all fields are initialized. As we saw with arrays, when one or more
initializers are missing, the structure elements will be assigned null values, zero for integers and
floating-point numbers, and null (‘\0’) for characters and strings.

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
4. Define a structure type personal, that would contain person name, date of joining and
salary. Write a program to initialize one person data and display the same.
Ans:

struct personal

char name[20];

int day;

char month[10];

int year;

float salary;

};

void main( )

Struct personal person = { “RAMU”, 10 JUNE 1998 20000};

printf(“Output values are:\n”);

printf(“%s%d%s%d%f”, person.name, person.day, person.month, person.year,


person.salary );

getch( );

Output:-

RAMU 10 JUNE 1998 20000

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
5. How to access the data for structure variables using member (‘.’) operator?Explain
with an example.
Ans:

We know that variables can be accessed and manipulated using expressions and operators. On
the similar lines, the structure members can be accessed and manipulated. The members of a
structure can be accessed by using dot(.) operator.

dot (.) operator

Structures use a dot (.) operator to refer its elements. Before dot, there must always be a structure
variable. After the dot, there must always be a structure element. The dot operator is also known
as member operator(period operator).

The syntax to access the structure members as follows,

structure_variable_name . structure_member_name

Consider the example as shown below,

struct student

char name [5];

int roll_number;

float avg;

};

struct student s1= {“Ravi”, 10, 67.8};

The members can be accessed using the variables as shown below,

s1.name --> refers the string “ravi”

s1.roll_number --> refers the roll_number

10 s1.avg --> refers avg 67.8

6. Define a structure type book, that would contain book name, author, pages and price.
Write a program to read this data using member operator (‘.’) and display the same.
Ans:

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
struct book

char name[20];

int day;

char month[10];

int year;

float salary;

};

void main( )

struct book b1; printf(“Input

values are:\n”);

scanf(“%s %s %d %f”, b1.title, b1.author, b1.pages,

b1.price); printf(“Output values are:\n”);

printf(“%s\n %s\n %d\n %f\n”, b1.title, b1.author, b1.pages, b1.price);

getch( );

Output:-

Input values are: C& DATA STRUCTURES KAMTHANE 609 350.00 Output

values are:

C& DATA STRUCTURES

KAMTHANE

609 350.00

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
7. How to pass a structure member as an argument of a function? Write a program to
explain it.
Ans:
Structures are more useful if we are able to pass them to functions and return them.

By passing individual members of structure

This method is to pass each member of the structure as an actual argument of the function call.
The actual arguments are treated independently like ordinary variables. This is the most
elementary method and becomes unmanageable and inefficient when the structure size is large.

#include<stdio.h>
main ( )
{
float arriers (char *s, int n, float
m); typedef struct emp
{
char name[15];
int emp_no;
float salary;
}record;
record e1 = {"smith",2,20000.25};
e1.salary = arriers(e1.name,e1.emp_no,e1.salary);
}
float arriers(char *s, int n, float m)
{
m = m + 2000;
printf("\n%s %d %f ",s, n,
m); return m;
}
Output
smith 2 22000.250000

8. How to pass an entire structure as an argument of a function? (or)


Write a program to pass entire structure as an argument of a

structure. Ans:

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
Structures are more useful if we are able to pass them to functions and return them.

Passing Whole Structure

This method involves passing a copy of the entire structure to the called function. Any changes
to structure members within the function are not reflected in the original structure. It is therefore,
necessary for the function to return the entire structure back to the calling function.

The general format of sending a copy of a structure to the called function is:

return_type function_name (structure_variable_name);

The called function takes the following form:

data_typefunction_name(struct_typetag_name)

………

………

return(expression);

The called function must be declared for its type, appropriate to the data type it is expected to
return.

The structure variable used as the actual argument and the corresponding formal argument in the
called function must of the same struct type.

The return statement is necessary only when the function is returning some data back to the
calling function. The expression may be any simple variable or structure variable or an
expression using simple variables.

When a function returns a structure, it must be assigned to a structure of identical type in the
calling function. The called functions must be declared in the calling function appropriately.

#include <stdio.h>

#include <string.h>

struct student

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
{

int id;

char name[20];

float percentage;

};

void func(struct student record);

int main()

struct student record;

record.id=1;

strcpy(record.name, "Raju");

record.percentage = 86.5;

return 0;

voidfunc(struct student record)

printf(" Id is: %d \n", record.id); printf("

Name is: %s \n", record.name);

printf(" Percentage is: %f \n", record.percentage);

Output:

Id is: 1

Name is: Raju

Percentage is: 86.500000

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
9. Write a program for illustrating a function returning a structure.
typedef struct
{
char name [15];
intemp_no;
float salary;
} record;
#include<stdio.h.>
#include<string.h>
void main ( )
{
record change (record);
record e1 = {"Smith", 2, 20000.25};
printf ("\nBefore Change %s %d %f
",e1.name,e1.emp_no,e1.salary); e1 = change(e1);
printf ("\nAfter Change %s %d %f ",e1.name,e1.emp_no,e1.salary);
}
record change (record e2)
{
strcpy (e2.name,
"Jones"); e2.emp_no =
16; e2.salary = 9999;
return e2;
}
Output:
Smith 2 20000.25
Jones 16 9999.99
10. What is an array of structure? Declare a variable as array of structure and initialize it?
(or)
When the array of structures is used? Write syntax for array of structure.

Ans:

An array is a collection of elements of same data type that are stored in contiguous memory
locations. A structure is a collection of members of different data types stored in contiguous
memory locations. An array of structures is an array in which each element is a structure. This
concept is very helpful in representing multiple records of a file, where each record is a
collection of dissimilar data items.

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
As we have an array of integers, we can have an array of structures also. For example, suppose
we want to store the information of class of students, consisting of name, roll_number and
marks, A better approach would be to use an array of structures.

Array of structures can be declared as follows,

struct tag_name arrayofstructure[size];

Let’s take an example, to store the information of 3 students, we can have the following structure
definition and declaration,
struct student
{
char name[10];
int rno;
float avg;
};

struct student s[3];

Defines array called s, which contains three elements. Each element is defined to be of type
struct student.

For the student details, array of structures can be initialized as follows,

struct student s[3]={{“ABC”,1,56.7},{“xyz”,2,65.8},{“pqr”,3,82.4}};


Ex: An array of structures for structure employee can be declared as

struct employee emp[5];

Let’s take an example, to store the information of 5 employees, we can have the following
structure definition and declaration,

struct employee
{
int empid; char
name[10];
float salary;
};
struct employee emp[5];

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
Defines array called emp, which contains five elements. Each element is defined to be of type
struct employee.

For the employee details, array of structures can be initialized as follows,

struct employee emp[5] =


{{1,”ramu”25,20000}, {2,”ravi”,65000},
{3,”tarun”,82000},
{4,”rupa”,5000},
{5,”deepa”,27000}};

11. Write a C program to calculate student-wise total marks for three students using array
of structure.
Ans:

#include<stdio.h>

struct student

char rollno[10];

char name[20];

float sub[3];

float total;

};

void main( )

int i, j, total = 0;

struct student s[3];

printf("\t\t\t\t Enter 3 students

details"); for(i=0; i<3; i++)

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
printf("\n Enter Roll number of %d Student:",i);

gets(s[i].rollno);

printf(" Enter the name:");

gets(s[i].name);

printf(" Enter 3 subjects marks of each student:");

total=0;

for(j=0; j<3; j++)

scanf("%d",&s[i].sub[j]);

total = total + s[i].sub[j];

printf("\n******************************************");

printf("\n\t\t\t Student details:");

printf("\n******************************************");

for(i=0; i<n; i++)

printf ("\n Student %d:",i+1);

printf ("\n Roll number:%s\n Name:%s",s[i].rollno,s[i].name);

printf ("\nTotal marks =%f", s[i].total);

getch( );

12. Write a C program using array of structure to create employee records with the
following fields: emp-id, name, designation, address, salary.

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
Ans:

#include<stdio.h>

struct employee

int emp_id;

char name[20];

char designation[10];

char address[20];

float salary; }emp[3];

void main( )

int i;

printf("\t\t\t\t Enter 3 employees

details"); for(i=0; i<3; i++)

scanf(“%d”,&emp[i].emp_id);

gets(emp[i].name);

gets(emp[i].designation);

gets(emp[i].address);

scanf(“%f”,&emp[i].salary);

printf("\n******************************************");

printf("\n\t\t\t Employee details:");

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
printf("\n******************************************");

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

printf(“%d”,emp[i].emp_id);

puts(emp[i].name);

puts(emp[i].designation);

puts(emp[i].address);

printf(“%f”,emp[i].salary);

getch( );

13. What is structure within structure? Give an example for it. (or)
Write a C program to illustrate the concept of structure within structure.

Ans:

Nested Structure (Structure within structure)

A structure which includes another structure is called nested structure or structure within
structure. i.e a structure can be used as a member of another structure. There are two methods for
declaration of nested structures.

(i) The syntax for the nesting of the structure as follows

struct tag_name1

type1 member1;

…….

…….

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
};

struct tag_name2

type1 member1;

……

……

struct tag_name1 var;

……

};

The syntax for accessing members of a nested structure as follows,

outer_structure_variable . innerstructurevariable . membername

(ii) The syntax of another method for the nesting of the structure is as follows

struct structure_nm

{
<data-type> element 1;
<data-type> element 2;
-----------
-----------
<data-type> element n;

struct structure_nm
{
<data-type> element 1;
<data-type> element 2;
-----------
-----------
<data-type> element
n; }inner_struct_var;
}outer_struct_var;

Example :

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
struct stud_Res
{
int rno;
char nm[50];
char std[10];
struct stud_subj
{
char subjnm[30];
int marks;
}subj;
}result;
In above example, the structure stud_Res consists of stud_subj which itself is a structure with
two members. Structure stud_Res is called as 'outer structure' while stud_subj is called as 'inner
structure.'
The members which are inside the inner structure can be accessed as follow
: result.subj.subjnm
result.subj.marks

Program to demonstrate nested


structures. #include <stdio.h>
#include <conio.h>
struct stud_Res
{
int rno;
char std[10];
struct stud_Marks
{
char subj_nm[30];
int subj_mark;
}marks;
}result;
void main()
{
clrscr();
printf("\n\t Enter Roll Number :
"); scanf("%d",&result.rno);
printf("\n\t Enter Standard : ");
scanf("%s",result.std);
printf("\n\t Enter Subject Code : ");

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
scanf("%s",result.marks.subj_nm); printf("\n\t
Enter Marks : ");
scanf("%d",&result.marks.subj_mark);
printf("\n\n\t Roll Number : %d",result.rno);
printf("\n\n\t Standard : %s",result.std);
printf("\nSubject Code : %s",result.marks.subj_nm);
printf("\n\n\t Marks : %d",result.marks.subj_mark);
getch();
}
Output:
Enter roll number : 1
Enter standard :Btech
Enter subject code : GR11002
Enter marks : 63
Roll number :1
Standard :Btech
Subject code : GR11002
Marks : 63

14.Write a C program using nested structures to read 3 employees details with the following
fields; emp-id, name, designation, address, da ,hra and calculate gross salary of each
employee.
Ans:

#include<stdio.h>

struct employee

int emp_id;

char name[20];

char designation[10];

char address[20];

struct salary

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
{

float da;

float hra;

}sal;

}emp[3];

void main( )

int i;

printf("\t\t\t\t Enter 3 employees

details"); grosssalary = 0;

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

scanf(“%d”,&emp[i].emp_id);

gets(emp[i].name);

gets(emp[i].designation);

gets(emp[i].address);

scanf(“%f”,&emp[i].sal.da);

scanf(“%f”,&emp[i].sal.hra);

grosssalary = grosssalary + emp[i].sal.da + emp[i].sal.hra;

printf("\n******************************************");

printf("\n\t\t\t Employee details with gross salary:");

printf("\n******************************************");

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

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
{

printf(“%d”,emp[i].emp_id);

puts(emp[i].name);

puts(emp[i].designation);

puts(emp[i].address);

printf(“%f”,emp[i].sal.da);

printf(“%f”,emp[i].sal.hra);

printf(“%f”,grosssalary);

getch( );

15. Distinguish between Arrays within Structures and Array of Structure with examples.
Ans:

Arrays within structure

It is also possible to declare an array as a member of structure, like declaring ordinary variables.
For example to store marks of a student in three subjects then we can have the following
definition of a structure.

struct student

char name [5];

int roll_number;

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
int marks [3];

float avg;

};

Then the initialization of the array marks done as follows,

struct student s1= {“ravi”, 34, {60,70,80}};

The values of the member marks array are referred as follows,


th
s1.marks [0] --> will refer the 0 element in the marks

s1.marks [1] --> will refer the 1st element in the marks
s1.marks [2] --> will refer the 2ndt element in the marks

Array of structure
An array is a collection of elements of same data type that are stored in contiguous memory
locations. A structure is a collection of members of different data types stored in contiguous
memory locations. An array of structures is an array in which each element is a structure. This
concept is very helpful in representing multiple records of a file, where each record is a
collection of dissimilar data items.

Ex:

An array of structures for structure employee can be declared as

struct employee emp[5];

Let’s take an example, to store the information of 5 employees, we can have the following
structure definition and declaration,

struct employee
{
int empid; char
name[10];
float salary;
};
struct employee emp[5];
Defines array called emp, which contains five elements. Each element is defined to be of type
struct student.

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
For the student details, array of structures can be initialized as follows,
struct employee emp[5] = {{1,”ramu”25,20000},
{2,”ravi”,65000},
{3,”tarun”,82000},
{4,”rupa”,5000},
{5,”deepa”,27000}};

16. Write a C program using structure to create a library catalogue with the following fields:
Access number, author’s name, Title of the book, year of publication, publisher’s name,
and price.
Ans:
struct library

intacc_no;

char author[20];

char title[10];

intyear_pub;

charname_pub[20];

float price;

};

void main( )

struct library lib;

printf(“Input values are:\n”);

scanf(“%d %s %s %d %s%f”, &lib.acc_no, lib.author, lib.title, &lib.year_pub,


lib.name_pub, &lib.price);

printf(“Output values are:\n\n”);

printf(“Access number = %d\n Author name = %s\n

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
Book Title = %s\n Year of publication = %d \n Name of publication = %s\n Price = %f ”,

lib.acc_no, lib.author, lib.title, lib.year_pub, lib.name_pub, lib.price);

getch( );

17. What is self referential structure? Explain through example.


Ans:

Self-referential structure

A structure definition which includes at least one member as a pointer to the same structure is
known as self-referential structure.

It can be linked together to form useful data structures such as lists, queues, stacks and

trees. It is terminated with a NULL pointer (0).

The syntax for using the self referential structure as

follows, struct tag_name

Type1 member1;

Type2 member2;

……….

struct tag_name

*next; };

Ex:-

struct node

int data;

struct node *next;

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
} n1, n2;

18. Explain unions in C language? Differentiate structures and unions.


Ans:
A union is one of the derived data type. Union is a collection of variables referred under a single
name. The syntax, declaration and use of union is similar to the structure but its functionality is
different.
The general format or syntax of a union definition is as follows,

Syntax:

union union_name

<data-type> element 1;

<data-type> element 2;

………………

}union_variable;

Example:

union techno

int comp_id;

char nm;

float sal;

}tch;

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
A union variable can be declared same way as structure variable.

union tag_name var1, var2...;

A union definition and variable declaration can be done by using any one of the following

We can access various members of the union as mentioned below, and memory organization is
shown below,

a.c

a.i

a.f

In the above declaration, the member f requires 4 bytes which is the largest among the members.
Figure 5.8 shows how all the three variables share the same address. The size of the union here is
4 bytes.

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
A union creates a storage location that can be used by any one of its members at a time. When a
different member is assigned a new value, the new value supersedes the previous members’
value.

Difference between structure and union:-

Structure Union

(i) Keyword Struct union

(ii) Definition A structure is a collection of A union is a collection of

logically related elements, logically related elements,

possibly of I different types, possibly of different types,


having a single name, shares
having a single name.
single memory location.

union tag_name
(iii) Declaration struct tag_name
{
{
type1 member1;
type1 member1;
type1 member2;
type1 member2;
……………
……………
};
};
union tag_name var;
struct tag_name var;
Same.
( iv) Initialization Same.
Accessed by specifying
(v) Accessing Accessed by specifying
unionvariablename. member
Structure variablename . member

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------
Name name

(vi)Memory Allocation Each member of the structure

occupies unique location, stored in Memory is allocated by

contiguous locations. considering the size of largest

member. All the members

share the common location

(vii) Size Size of the structure depends Size is given by the size of

on the type of members, adding largest member storage.

size of all members. sizeof(un_variable)

sizeof (st_var);

(viii) Using pointers Structure members can be same as structure.

accessed by using dereferencing

operator dot and selection

operator(->)

We can have arrays as a member of We can have array as a member

structures. All members can be of union. Only one member can

accessed at a time. be accessed at a time.

Nesting of structures is possible. same.

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT


Q&A for Previous Year Questions Subject: CP (B.Tech. I Year) Subject Code: CS204ES
UNIT-III
-----------------------------------------------------------------------------------------------------------------------------------------

It is possible structue with in It is possible union may contain


structure as a member
union as a member.

J.NARESH KUMAR, ASST.PROFESSOR, IT DEPT, GNIT

You might also like