0% found this document useful (0 votes)
3 views5 pages

unit 4 qp

The document provides a comprehensive overview of structures and unions in C programming, detailing their definitions, syntax, and various operations such as declaration, initialization, and memory allocation. It covers concepts like nested structures, self-referential structures, and dynamic memory allocation functions including malloc, calloc, and realloc. Additionally, it includes examples and exercises related to structures, emphasizing their usage in real-world programming scenarios.
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)
3 views5 pages

unit 4 qp

The document provides a comprehensive overview of structures and unions in C programming, detailing their definitions, syntax, and various operations such as declaration, initialization, and memory allocation. It covers concepts like nested structures, self-referential structures, and dynamic memory allocation functions including malloc, calloc, and realloc. Additionally, it includes examples and exercises related to structures, emphasizing their usage in real-world programming scenarios.
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/ 5

4931_Grace College of Engineering,Thoothukudi

STRUCTURES AND UNION


Structure - Nested structures Pointer and Structures Array of structures Self referential
structures Dynamic memory allocation - Singly linked list typedef Union - Storage classes
and Visibility
UNIT-IV / PART-A
1. What is a structure? (or) State the meaning of the root word struct. (May 15,18)
a User defined data type
Can hold many data objects of different data types (heterogeneous) may contain the
integer elements, float elements and character elements. etc.
Collection of variables under single name
Can conveniently used to represent a record
2. Give syntax for structure definition
Syntax:
[storage class specifier][data type] struct [structure name]
{
Data _type memeber_name[, member name 2,..];
Data _type memeber_name[, member name 2,..];
}[variable name];
Example:
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
3. Define structure declaration.
Variables/constants for structure types can be declared at definition or after
definition
[storage class specifier] struct named_Structure-type identifier name
[=initialization list];
Struct key word mandatory
A structure must end with a semicolon
Example: Declare variables Book1,Book2 of type Books
struct Books Book1;
struct Books Book2;
4. Write the rules for declaring a structure?
A structure must end with a semicolon
Struct key word mandatory
Each structure member must be terminated.
The structure variable must be accessed with dot(.) operator.
Structure decaration list (structure members):
1. Can have char, float, double, int, array[], pointer* other structure types
2. Cannot have void, function type, same structure instance
3. Can have pointer to an instance of itself which is called as self referential
structures.
5. Write the three ways to pass structure variables.
Pass each member of the structure as an actual argument of the function call.
Pass a copy of the entire structure to the called function
Through pointer pass the structure as an argument.

CS3251_PIC
4931_Grace College of Engineering,Thoothukudi

6. How structure elements can be accessed?


Structure members can be accessed using
1. Direct member access operator/dot operator
Represented as (.)
a Binary Operator
Syntax: Structure_name.structure_member_name
2. Indirect member access operator/arrow operator
Represented as (->)
to access structure members by the pointer to the structure
Syntax: Pointer_to_Structure->structure_member_name
7. Differentiate between array and structure. (OR) Compare and contrast a structure
with an array (May 19)
Array Structure
An array is a collection of variables of A structure is a collection of variables
same data type of different data types
An array is a derived data type It is a user defined data type.
The individual data members of an array The individual data members of the
can be initialized. structure cannot be initialized.
Array elements can be accessed by Structure members can be accessed using
indexing the array name. dot operator / arrow operator
8. Give rules for initializing structure
The individual data members of the structure cannot be initialized.
The structure variables can be initialized at compile time only.
The order of data members in a structure must match the order of values in enclosed
brackets.
We can initialize only some of the data members of the structure.
The uninitialized data members can be initialized by default with zero(0) for int and
float 0.0 and \ for character and strings.
9. Define nested structure.
Structure within another structure is called as nested structure
Used to create complex data types
Nested Structures contain declaration of members of other structure types
We can also define a structure within declaration list of another structure
Double braces used to initialize nested structure objects
Eg: emp={name,salary,{date of birth}};
It is used to increase the readability of the program by reducing the complexity.
10. Define the Structure called ID_card to hold the details of the student. (Jan 16)
struct ID_card
{
char name[50];
char address[50];
int age;
} b1,b2;
11. What are self-referential structures?
A structure consisting of at least a pointer member pointing to the same structure is
known as self-referential structure.
Example: struct Books
{
int book_id;
struct Books* ptr; };//ptr is a pointer pointing to structure type Books

CS3251_PIC
4931_Grace College of Engineering,Thoothukudi

12. Write the syntax of pointers to structures.


A pointer can be declared in such a way that it points to a structure data type. A pointer
to a structure is created as follows
struct student
{
int rno;
char name[23];
float avg;
};
struct student *str;
13. Difference between array and structures.
Array Structures
Array elements are of same data types Structure elements are of different data
(Homogeneous). type (heterogeneous).
Array allocates static memory and uses Structures allocate dynamic memory and
index / subscript for accessing elements uses (.) operator for accessing the member
of the array. of a structure.
Array is a pointer to the first element of it. Structure is not a pointer
An array is a derived data type A structure is a user
user-defined
defined data type
Example for Array Declaration Example for Structure Declaration
int b1[3]; struct Books {
int a; char b;
}b1;
14. Explain typedef with syntax and an example. (or) Specify the use of typedef. (Nov 18)
typedef is a keyword used in C language to assign alternative names to existing types.
Its mostly used with user defined data types, when names of data types get slightly
complicated.
Syntax:
typedef existing_name alias_name
Example:
typedef unsigned long ulong;
15. Explain array of structures with an example.
Declaring an array of structure is same as declaring an array of fundamental types. Since
an array is a collection of elements of the same type. In an array of structures, each
element of an array is of the structure type.
Example:
struct car
{
char make[20];
char model[30];
int year;
};
16. Define Singly Linked list.
Singly Linked Lists are a type of data structure. It is a type of list. In a singly linked
list each node in the list stores the contents of the node and a pointer or reference to the
next node in the list. It does not store any pointer or reference to the previous node.
Example:
struct node{
int data;
struct node *next; };

CS3251_PIC
4931_Grace College of Engineering,Thoothukudi

17. Define dynamic memory allocation functions and list its types.
The process of allocating memory during program execution is called dynamic memory
allocation.
C language offers 4 dynamic memory allocation functions. They are,
1. malloc()
2. calloc()
3. realloc()
4. free()
18. Explain malloc with its syntax.
malloc () function is used to allocate space in memory during the execution of the
program.
malloc () does not initialize the memory allocated during execution. It carries
garbage value.
malloc () function returns null pointer if it able to allocate requested amount
of memory.
Syntax
malloc (number *sizeof(int));
19. Explain calloc with its syntax.
It allocates multiple blocks of requested memory. calloc () initializes the allocated
memory to zero
Calloc () function is also like malloc () function. But calloc () initializes the allocated
memory to zero. But, malloc()
Syntax
calloc (number, sizeof(int));
20. Explain realloc with its syntax.
realloc () function modifies the allocated memory size by malloc () and calloc ()
functions to new size.

allocated for the full size of reallocation, then copies the existing data to new block
and then frees the old block.
Syntax
realloc (pointer_name, number * sizeof(int));
21. Explain free function with example.
free () function frees the allocated memory by malloc (), calloc (), realloc () functions and
returns the memory to the system.
Syntax: free (pointer_name);
22. What is the output of the following code fragment? (May 19)
struct point
{ int x;
int y;
}; struct point origin, *pp;
main()
{
pp=& origin;
printf("origin is (%d%d)\n",(*pp).x,pp->y);
} Output: origin is (00)
UNIT-IV / PART-B
1. What is a structure? Create a structure with data members of various types and declare
two structure variables. Write a program to read data into these and print the same.
Write short notes on structure Declaration. (Jan 16)

CS3251_PIC
4931_Grace College of Engineering,Thoothukudi

2. Write a program to print student grade using structure / Write a C program to create a
mark sheet for students using structure. (Jan 14)
3. (i) Write short notes on nested structure / Explain the concept of structure within
structure with suitable program.
(ii) Define and declare a structure to store date, which including day, month and year.
(Jan 14)
4. Define a structure called book with book name, author name and price. Write a C
program to read the details of book name, author name and price of 200 books in a
library and display the total cost of the books and book details whose price is above
Rs.500. (May 15)
5. Write a C program to store the employee information using structure and search
a particular employee using employee number? (Jan 14) (May 14)
6. Explain dynamic memory allocation in detail (or) What is dynamic memory allocation?
Explain various C functions that are used for the same with examples. (May 19)
7. i) Difference between static memory allocation and dynamic memory allocation in c.
ii) Difference between malloc( ) and calloc( ) functions in c.
8. Write a C program to implement singly linked list using dynamic memory allocation.
9. Define structure in C. Also specify the pointer and structure with example. (May 18)
10. i) Write a C program for accessing structure member through pointer using dynamic
memory allocation. (May 18)
ii) Write a short note on singly linked list and specify how the nodes are created in
singly linked list. (May 18)
11. What are self -referential structures? Explain with suitable examples. (May 19)

CS3251_PIC

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy