0% found this document useful (0 votes)
5 views

UNIT-3 (1)

M31-1
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)
5 views

UNIT-3 (1)

M31-1
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/ 15

UNIT-III

STRUCTURE :

Def:
A structure is a collection of data items of different data types under a .single name. A structure is a collection of
heterogeneous data items
A structure is declared as follows:

struct tag
{
data type member-1;
data type member –2;
.
.
.
data type member n;
};
 In this declaration struct is a keyword.
 Fag is the name given to the structure declared.
 Data type can be integer, float, char or any other data types.

structure can also be declared as

struct tag
{
type 1 data 1;
type 2 data 2;
type n data n;
} variable 1, variable 2 , .... variable n;

Accessing members of structure :


Structure members can be accessed by writing variable_ name. member;
Dot(.) operator links structure variable to the data member. It is highest precedence operator
and associativity is left to right,

Assigning values to structure :


 One way is to assign values at the time of declaring structure variables.

Prepared by : M VB REDDY 36
struct person P1 = {"Ravi", 21, "Male", "Student"}
 Another way is to assign values to each member separately as shown below
strcpy(P1. name, "Ravi");
P1.age =21;
strcpy(P1.gender,"malc");
strcpyl P1. occupation, "student");

Arrays within structures

Suppose you modify the student structure such that it also contains fields for marks in all subjects, total and
average. Let there be six subjects, in the curriculum it will not be an efficient way to use six different
variables, instead an array of marks can be used.
C permits use of arrays as structures members. We can use single dimensional arrays or multidimensional
arrays as members.

Consider the following structure


Struct student
{
char name [25];
int rollno;
char branch[25];
int marks [6];
int total.aug;
}S;
Here the character array name and branch are used to store name of student and branch of study. The
member marks contains 6 elements marks[0] to marks[5]. They are accessed as given below.
S.mark [0] refer to marks obtained in first subject S.marks[1] refers to marks obtained in second subject
and similarly S.marks[5] refers to marks obtained in sixth subject.
Array of structures :

In previous example you have seen how to access data of a particular student. Now suppose there are 20
students whose details have to be accessed. Definitely using different structure variables is difficult. In such
situations array of structures is used.
Example:
Struct student
{
char name[20];

Prepared by : M VB REDDY 37
int rollno;
int mark[3];
int total;
}S[20];
Here S[0].name, S[0].rollno, S[0].mark[j] refer to name, rollno and marks in jth subject of first student. In
general S[I].name, S[I].rollno and so on refer to details of (i+1)th student.
Structures within structures
Consider a structure department of a college. Each department will have name associated with it like
computer science department, mechanical department etc. each department will also have staff. Staff inturn
has attributes for name and designation as shown below. Here both department and staff are structures.
In such cases nesting of structures is used. Structures for department and staff are shown below:
Struct department
{
char name[25];
struct staff S[20];
}d;
struct staff
{
char name[25];
char designation [25];
}S;

Here the details of staff of a particular department can be accessed as shown below
d.S[i].name : it gives name of ith staff in a department
d.S[i].designation : it gives designation of ith staff in a department
where i is a numerical constant.

Different ways of passing structures to functions

There are different ways of passing structure type arguments to functions. These are given below:
1. Passing structure members individuality

Each member of structure can be passed individually as arguments in the function call and retained through
return statement.
2. Pass the address of structure to the called function :
This method is more efficient then the second method because structure need not be returned to called
function

Prepared by : M VB REDDY 38
Unions :

A union is a collection of data items of different data types. It is similar to that of structure.
A union is declared as shown below.
Union
{
data type member 1;
data type member 2;
.
.
.
data type member n;
};
 Here union is a keyword
 Member 1, member 2, member n are members of union.
 The variables of union are declared as
union variable 1, variable 2………, variable n;
Declaration of union variables and union is similar to that of structure variables and structures respectively.

Pointers

Pointers are special variables which store address of some other variables or values instead of storing
normal values.
Pointers are declared as :
datatype *ptr;
Here * indicates that ptr is a pointer variable pointing to a datatype.

Initialization of pointer variable

Pointer variable can be initialized during declaration as given below:


int *p;
*p=5;
on compilation of the above statement, the value 5 is stored in an address pointed by p. suppose p points to
an address .so 5 is stored in location 1540.

Assigning of address of variable to pointer variables :

Prepared by : M VB REDDY 39
A pointer variable is used to point the addresses of other variables.
Example :
int *P;
int x=5;
P = &x;
Here address of variable x is assigned to pointer P.

Operations that can be performed on pointers are

1. A pointer variable can be assigned value NULL (zero)


2. An integer can be added to or subtracted from pointer variable.
3. One pointer variable can be subtracted from another provided both pointers point to element of the same
array.
4. Two pointer variables can be compared provided both point to objects of same datatype. All the
relational operations. <, <=, >, >=, = =, != can be used for comparison

Array of pointers and pointer to arrays:

Multidimensional arrays can be represented in terms of array of pointers and pointer to arrays as shown
below.

Pointer to arrays
Consider a two dimensional array.
data-type array-name [row-size] [co-size];
this can be represented using pointer to arrays as.

datatype(*ptr)[co-size]

This declares ‘ptr’ to be a pointer to contiguous one dimensional arrays. Similarly n-dimensional array is
declared using pointer variable as shown below
datatype(*ptr) [dim 2 ] [dim 3 ] ……..[dim n];
Where dim 2,dim 3,…………. dim n give the sizes of each dimension.

Array of pointers

A two dimensional array can be expressed in terms of array of pointers as given below :

Prepared by : M VB REDDY 40
datatype *ptr[dim 1]
This is same as the declaration
datatype array[dim 1] [dim 2];
similarly a multidimensional array can be expressed as
data type array [dim 1 ] [dim 2] ……..[dim n-1];
FILE
A File is a place on disk where a group of related data is stored.
It is advantageous to use files in the following circumstances.
1. when large volumes of data are to be handled by the program and
2. when the data need to be stored permanently without getting destroyed when program is terminated.

Basic operations on file

1. Naming a file
2. Opening a file
3. Reading data from file
4. Writing data to file
5. closing a file

Random access files:

In sequential access of file, data is read from or written to the sequentially, i.e.from beginning of file to end
of file in order. It is achieved using functions fpritf( ), fscanf( ), getc( ), putc( ),etc. in random access of file,
a particular part of a file can be accessed irrespective of other parts.
Random access of files is achieved with the help of functions fseek( ), ftell( ) and rewind ( ) are available
in I/O library. The functionality of each of these functions are given below.

Quiz Questions

1. Structure is a collection of
a) similar data items
b) dissimilar data items
c) can be both a and b

Prepared by : M VB REDDY 41
d) none
2. Memory. is allocated when
a) structure is declared.
b) members of structure are declared
c) structure variables are declared
d) none.

3. When structure variable are declared memory is allocated to


a) all members of structure
b) member-- equivalent to largest element is allocated
c) no memory is allocated
d) none
4. struct
{
int i;
float j;
size of S will be
a) 4 hues b) 2 bytes c) 6 bytes d) 0 bytes

5. Structure members should he initialized


a) within structure definition
b) outside structure definition
c) a or b
d) none
6. struct person
{
int no;
float val;
}P1;
a) 2 bytes b) o bytes c) 4 bytes d) none

7. Union is collection of
a) similar data items
b) dissimilar data items

Prepared by : M VB REDDY 42
c) a or b
d) none
8. Which of the following is true with structures?
a) all structure members can be initialized at a time
b) structure members cannot be initialized
c) only a single structure member can be initialized at a time
d) none
9. Memory is allocated when
a) union variables are declared
b) union is defined
c) both a and b
d) no memory is allocated.
10. Which of the following is true '.'
a) all union members can be initialized at a time
b) all union members cannot be initialized at a time c ) both a and b
d) no memory is allocated.
11. Which of follow ing is true for same number and type of structure and union members.
a) structure variable occupies more memory
b) union variable occupies more memory c ) both occupy same
d) none
12 Pointer variable store
a) a value b) a and b c) an address d) cannot be determined
13. int*P;
The size of *P is
a) 2 bytes b) 4 bytes c) 1 byte d) cannot be determined
14. char *P .
The size of *P is
a) 2 bytes b) 4 bytes c) \ bytes d) cannot be determined

15. float *q;


sizeof(*q) is
a) 2 bytes b) 4 bytes c) 6 bytes d) none
16. float *q;
size of q is
a) 2 bytes b) 4 bytes c) 6 bytes d) none
17.int *P = 5 ;

Prepared by : M VB REDDY 43
printf ("%d", P); What will be printed ?
a) Address b) 5 c) cannot be determined d) none
18. int *q= 10 ;
pnntf ("%d", *q) ; What will be printed ?
a) address b) 10 c) cannot be determined d) none
19. float *f;
If f stores address 500 what will be the value of f + 5 ?
a) 500 b) 512 c) 516 d) 520
20. mt *p , *q ;
*p=10; *q=5;
p = q;
What will be the value of *p ?
a) 5 b) 10 c) address d) cannot be determined
21. struct
{
int i;
float j ;
} *S;
The size of (*S) is
a) 6 bytes b) 2 bytes c) 4 bytes d) none 22.
22.In the above question (2 1 ) What is size of S?
a) 6 bytes b) 2 bytes c) 4 bytes d) none
23. union
{
int i;
float j ;
} *S;
Size of *S is
a) 4 bytes b) 2 bytes c) 6 bytes d) none

24. From the above question (23) what is size of S


a) 4 bytes b) 2 bytes c) 6 bytes d) none
25. "r" is used to
a) read already existing file
b) write already existing file
c ) read and write existing file

Prepared by : M VB REDDY 44
d) append existing file
26. "a+'" is used to
a) write into a file
b) append a file
c ) reading and appending a file d ) none
27. "r+" is used to open a file in
a) read mode b) read and write mode c) write mode d) append a file
28. fseek (fp, 0L., 1) is used to
a) goto beginning of the file
b) move backward c ) move forward
d) stay at current location.
29. int X[ 10] [201;
X can be declared using pointer to arrays as
a) int (*x)[20];
b) int (*x)[10];
c) int *x[20];
d) mt *x[10];
30. int y [10][20]; can be declared using array of pointers as
a) int *y[20];
b) int *y[10];
c) int (*y)[10];
d) int (*y)[20];
31. What should be the value of position to indicate end of file in fseek (fp, offset, position);
a) 0 b) 1 c) 2 d) 3
32. What should be the value of the position to indicate beginning of the file ?
a) 0 b) 1 c) 2 d) 3
33. What should be value ot position to indicate current position?
a) 0 b) 1 c) 2 d) 3
34. fseek(fp, - i, 1);
a) Moves fp backward by i bytes from end of file
b) Moves fp forward by i bytes from end of file
c) Moves fp backward by i bytes from current location
d) fp remain as it is
35. feek(fp, i, 2);
a) Moves fp backward by i bytes from the current position
b) Moves fp forward by i bytes from the current position

Prepared by : M VB REDDY 45
c) Moves fp forward by i bytes from the end of file
d) return -1; fp cannot be moved.
36. Which of the following is true?
a) A file can be modified if it is opened in read mode
b) fseek( ) is used in sequential access
c) A file can be opened after it is closed
d) A file cannot be opened after it is closed
37. fseek is used in
a) random access c) linked access
b) sequential access d) none
38. ftell(fp);
Gives position in the file in terms of
a) number of bytes
b) number of bits
c) number of characters
d) number of words.
39. rewind(fp);
a) Resets fp to start of file
b) Resets fp to end of file
c) fp remains unchanged
d) none
40. int (*f)( )
{
}

This function is
a) pointer to a function c) a) and b)
b) function that returns pointer d) none
41. In call-by-referencc. address of actual and formal parameter will be
a) same b) different c) depends on usage d) none
42. int *f( )
{
}
Here f is
a) pointer to function h) function that returns pointer
c) cannot be determined d) function that returns integer.

Prepared by : M VB REDDY 46
43. float * p ;
The type of p is
a) int b) float c) char d) none
44. float *p ;
The type of *p is
a) int b) float c) char d) none
45. The function of putw( )is
a) writes integer to file
b) writes an integer to console
c) write string to file
d) writes string to console.
46. The function getc ( ) is used
a) to read characters from file
b) to read characters from console
c) to read integer from file
d) to read integer from console.
47. fseek(fp, i, 0);
a) Moves fp to (i + i)th position from beginning
b) Moves fp backward from current location by i bytes
c) Moves fp forward from current location by i bytes.
48. struct
{
int k;
char c;
} * P1;
If the address stored in PI is 1000 what is the value of P1 + 3
a) 1009 b) 1006 c) 1000 d) 1003

Prepared by : M VB REDDY 47
Previous Questions
1. How to compare structure variables ? Give an example.
2. Define a structure type struct ABS, that contains name, age, designation and salary.
Using this structure, write a C program to read this information for one person from the
keyboard and print the same on the screen.
3. Write a C program to find number of words, blank spaces, special characters, digits
and vowels of a given text using pointers.
4. Write a C program to illustrate the comparison of structure variables
5. What is the use of a structure? Give an example for a structure with initialized values.
6. Write a C program using pointer for string comparison.
7. Write a C program to arrange the given numbers in ascending order using pointers.
8. When are array of structures used? Declare a variable as array of structure and
initialize it.
9. Write a C program to calculate student-wise total for students using array of structure.
10. Write a C program to find factorial of a given number using pointers
11. Write a C program to arrange the given names in alphabetical order using pointers
12. What is structure within structure ? Give example for it
13. write a C program to illustrate the concept of structure within structure
14. Write a C program to find out the presence of a substring in a given string using
pointers.
15. Write a C program to find the length of a given string using pointers. OR
Write a C program using pointers to determine the length of character string.
16. (a) Explain the different ways of passing structure as arguments in functions.
(b) Write a C program to illustrate the method of sending an entire structure as a parameter to a
function.
17. How to use pointer variables in expressions? Explain through examples.
18. Write a C program to illustrate the use of pointers in arithmetic operations.
19.List out the similarities and differences between structures and unions.
20. What is the general format of a union. Declare a union and assign values to
it. Explain the process of accessing the union members.
21. Write a 'C' Program to compute the sum of all elements stored in an array
using pointers.

Prepared by : M VB REDDY 48
22. What is a structure? Describe the governing rules for declaring a structure.
23. Write a C program to prepare marks memo of a class of students using structures.
24. How to use pointers as arguments in a function? Explain through an example.
25. Write a C function using pointers to exchange the values stored in two]
locations in the memory.
26. Explain with an example how a structure can be organized in the 'C' language.
27. Write a C program to print maximum marks in each subject along with the name of the student by
using structures. Take 3 subjects and 3 students records.
28.Explain the process of declaring and initializing pointers. Give an example.
29. Define a structure called 'cricket' that will describe the following information: player name, team
name, batting average using cricket. Declare an array player with 50 elements and write a C program
to read the information about all the 50 players and print a team-wise list containing names of players
with their batting average.
30.What is a pointer? List out the reasons for using pointers?
31 . Write a C Program to illustrate the use of indirection operator " * " to access the value
pointed by a pointer.
32. Define a structure that can describe a hotel. It should have members that include the name,
address, grade, average room charge and number of rooms.
Write a C program to perform following operations:
(i) To print out hotels of a given grade in order of charges.
(ii) To print out hotels with room charges less than a given value.
33. Explain the way of defining, opening and closing a file. Also explain the different modes of
operation.
34. Write a C program to read data from the keyboard, write it to a file called INPUT, again read
the same data from the INPUT file, and display it on the screen.
35. Design a structure to store length in kilometers, meters and centime Write a function to find
the addition of two measurements by passing measurements as arguments to a function
36. Explain the I/O operations on files
37.Write a C program to read numbers from a file DATA, contains a series of integer numbers,
and then write all odd numbers to a file to be called ODD and all even numbers to a file to be
called EVEN.
38. Consider a structure master includes the information like name, code, pay, experience. Write
a program to delete and display the information contained in master variables for a given code.
39. Distinguish between the following functions :
i. getc and getchar

Prepared by : M VB REDDY 49
ii. rewind(fp) and fseek (fp,OL)

40. Write a C program to change all upper case letters in a file to lower case letters and vice
versa.
41. What is a structure? How is it declared? How it is initialized?
42. Define a structure to represent a data. Use your structures that accept two different dates in
the format mm-dd of the same year. And do the following:
Write a C program to display the month names of both dates.
43. Write a C program to illustrate the use of structure pointer.
44. Explain the effects of the following statements:
(i) int a, *b = &a;
(ii) int p, *p;
(iii) char*S;
(iv) a=(float*)&X.
45.Explain the advantages of structure type over the array type variable.
46. Define a structure that represent a complex number (contains two floating point numbers
called real and imaginary). Write a C program to add, subtract, and multiply two complex
numbers.
47. The roots of a quadratic equation of the form ax2+bx+c = 0 are given by the following equations:
X1 = -b+rootof(b2-4ac)/2a X2 = -b-rootof (b2-4ac)/2a
Write a function to calculate the roots. The function must use two pointer parameters, one to receive
the coefficients a, b and c and the other to send the roots to the calling function.

Prepared by : M VB REDDY 50

You might also like