Latest News
|
|
UNIT 4
TOPICS TO BE COVERED IN THIS UNIT:
- Derived types - structures
- Declaration, definition and initialization of structures, accessing structures
- Nested structures
- Arrays of structures
- Structures and functions
- Pointers to structures
- Self referential structures
- Unions
- Typedef, bitfields
TEACHING MATERIAL
Unit 4 Teaching Aids - Click Here
TUTORIAL MATERIAL
Tutorial 1
- A structure is a -------------- data type.
- How do we declare a structure?
- Identify each part defined in the following structure
Struct book --------------->
{
Char title [15];------------------>
Char author [15]; ----------------->
Float price; ---------------------->
}book1, book2, book3; ------------------->
- We can access structure members by making use of -----------operator.
- What are the different ways used to declare structure variables?
- A structure is to be declared as -------when it is used inside a function.
- Declare a structure student with members as:
- Rollno
- branch
- Marks
- grade
- Declare the same structure as mentioned above and initialize the members with values and print them.
Tutorial 2
- If we declare an array as a structure variable then the members of the structure are accessed by array by making use of __________ operator.
- Give details of 4 students in a structure and access the details by defining array as structure variable?
- Struct marks
{
int numbers;
float subjects[3];
}
Student[3];
In the above structure how the member subjects will be accessed by the structure variable students?
- Define a structure employee and within that structure define another structure salary which gives the salary details of the employee?
- The numbers of a structure can be accessed using _______ operator when the structure variable is declared as pointer.
- Write a program to create a structure inventory which contains the following members
- name
- number
- price
Access the members of the structure by declaring a pointer as structure variable.
Tutorial 3
- If any member of a structure is pointer to the same structure then it is known as _________ structure.
- union marks
{
Int rollno ;
Float subjects[3];
Char name;
}
Student;
How many bytes of memory space is required for the above declared union?
- What is the difference between structure and union?
- By making use of typedef declare an identifier num for data type int.now declare two variables of type num?
- Write the general form of bitfield definition?
- Struct details
{
Unsigned int age=7;
Unsigned int children=3;
Unsigned gender=1;
};
What is the range of each member of the above structure?
PRACTICE PROBLEMS
Beginner Problems
- Write a program to use structure within structure. Display the contents of structure elements.
- Write a program to display the size of a structure.
- Find out errors if any in the following program:
#include
main()
{
struct xx
{
int x=3;
char name[]='hello';
};
struct xx s;
printf("%d",s.x);
printf("%s",s.name);
}
- Find out errors if any in the following code snippet:
struct emp
{
int ecode;
struct emp e;
};
- Differentiate structure and union.
Intermediate Problems
- Define a structure to represent a data. Use your structures that accept two different dates in
the format of mmdd.
And do the following: Write a C program to display the month names of both dates
- Write a program to create an array of student structure objects and to find the highest marks scorer.
The fields in the student structure are: name, age and marks.
- Find out the errors if any in the following code snippet:
typedef struct
{
int data;
NODEPTR link;
} *NODEPTR;
- Predict the output of the following program
#include
int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit;
clrscr();
printf("%d\n",sizeof(bit));
return 0;
}
- Predict the output of the following program
#include
#include
struct emp
{
int len;
char *name;
};
int main()
{
char newname[]='India';
char *buff=malloc(sizeof(struct emp)+strlen(newname)+1);
struct emp *p = (struct emp*)buff;
clrscr();
p->len=strlen(newname);
strcpy(p->name,newname);
printf("%d\t%s\n",p->len",p->name);
free(p);
return 0;
}
Advanced Problems
- Write a C program to read the information from the keyboard in which the “Employee” structure consists of
employee name, code, department (which is again a structure), address (another structure), designation
and salary. Construct an array of structures that stores ‘n’ employees information and write a program to
carry out operations like inserting a new entry, deleting entry.
- Write a C program to add, subtract and multiply the given complex numbers. Define functions and print with
structures as arguments.
- The annual examination is conducted for 50 students for ‘n’ subjects. Write a program to read the data and
determine the following:
- Total marks obtained by each student.
- The highest marks in each subject and the Roll Number of the student who secured it.
- The student who obtained the highest total marks.
-
- Predict the output of the following program and analyze it.
#include
int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit={1,2,2};
clrscr();
printf("%d\t%d\t%d\n",bit.bit1,bit.bit3,bit.bit4);
return 0;
}
- Predict the output of the following program and analyze it.
#include
#include
struct emp
{
int len;
char name[1];
};
int main()
{
char newname[]='CLanguage';
struct emp *p = (struct emp*)malloc(sizeof(struct emp)-1+ strlen(newname)+1);
clrscr();
p->len=strlen(newname);
strcpy(p->name,newname);
printf("%d\t%s\n",p->len,p->name);
return 0;
}
This Page Is Designed By Santosh
|