PC U4 2024
PC U4 2024
2- MARKS
C supports a constructed data type known as structures, a mechanism for packing data
of different types. A structure is a convenient tool for handling a group of logically related data
items. This is known as Structure.
• The order of data members in a structure must match the order of values in enclosed
brackets.
• The uninitialized data members can be initialized by default with zero for int and float ‘\0’ for
character and strings.
It is otherwise known as structure within structure, i.e., a structure appear within another
structure. This is called nested structure.
The values of a structure variable can be assigned to another structure variable of the same
type using the assignment operator. It is not necessary to copy the structure elements.
Arrays Structures
3. Any array behaves like a built-in 3. But in the case of structure, first
data types. All we have to do is to we have to design and declare a data
declare an array variable and use structure before the variable of that
it. type are declared and used.
struct structure_name
Structure_member 1;
Structure_member 2;
…….
Structure_member n;
}; v1,v2,…,vn;
Self referential structures are those structures that contain a reference to data of its
same type. That is it contains the pointer to the data that is of same type of as that of the
structure.
struct node
int val;
};
Here the structure node will contain two types of data- an integer val and next that is a
pointer
to a node.
struct books
{
char book_name[50];
int pages;
float price;
}b1;
Syntax:
typedef int numbers;
eg: numbers num1,num2;
Enumerated type:
This is also a user defined data type. “Enum” is the keyword and “identifier” is the user
defined data type that is used to declare the variables. It can have any value enclosed within the
curly braces.
Syntax:
Nesting of structures are nothing but structure within another structure (i.e., a
structure can contain one or more structures embers).
struct employee
int empno;
char name[15];
struct dob
int date ,
int month,
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
int year;
} d;
}emp;
1. It is a complex data type declaration that defines a physically grouped list of variables to
be placed under one name in a block of memory, allowing the different variables to be
accessed via a single pointer, or the struct declared name which returns the same
address.
2. For increasing the execution speed.
12. Write the syntax for declaration and initialization of structures. (JAN 2011)
Syntax:
struct structure_name
{
data type data member1;
struct book_bank
char bname[50];
char author[50];
int pages;
float price;
}book1;
The members of a structure themselves are not variables. They do not occupy any
memory until they are associated with the structure variables such as book1. When the compiler
comes across a declaration statement, it reserves memory space for the structure variables. It
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
is also allowed to combine both the structure definition and variables declaration in one
statement.
Array of pointers contain collection of address. The address may be the address of
variable or array elements. So far we have studied array of different standard data types such
as array of int, float, and character and so on.
In the same way the C language also supports array of pointers. It is nothing but a
collection of addresses. Here we store addresses of variables for which we have to declare an
array as a pointer.
24. Write a program using pointer to determine the length of a character strings?
main()
{
char *name;
int length;
char *cptr=name;
name=”DEHI”;
printf(“%s\n”,name);
while(cptr!=’\0’)
{
printf(“%c is stored at address %u\n”,*cptr,cptr);
cptr++;
}
length=cptr-name;
printf(“\nlength of the string=%d\n””,length);
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
It is a variable that contains the address of another variable. Similarly another pointer
variable can store the address of this pointer variable. This is known as Pointer to Pointer.
1. *
2. &
3. .
4. ->
The address-of operator (&) gives the address of its operand. The indirection operator
(*) accesses a value indirectly, through a pointer. The operand must be a pointer value. The
result of the operation is the value addressed by the operand; that is, the value at the address to
which its operand points.
PART -C
1. EXPLAIN ABOUT STRUCTURE WITH EXAMPLE PROGRAM. (May 2017, Jan
2014,May-2019]
DEFINITION
DECLARATION OF STRUCTURES
It contains data members and each is accessed by the structure variable. A structure is
declared using the keyword struct followed by a structure name. All the variables of the
structures are declared within the structure.
Syntax:
struct structure_name
structure_element 1;
structure_element 2;
--------
--------
structure_element n;
struct student
Example:
{
int marks;
float avg;
char grade;
};
The structure definition does not allocate any memory. Structure provides a model of how the
structure is to be in memory and gives details of the member names. Memory is allocated for
the structure when we declare a variable of the structure.
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
struct student
int marks;
float avg;
char grade;
int marks
float avg
avg grade
INITIALIZATION OF STRUCTURES
Initializing a structure means assigning some constants to the members of the structure. The
initializes are enclosed in braces and are separated by commas.
Example:
struct student
int r_no;
char name[20];
char course[20];
float fees;
Array elements are accessed using the Subscript variable, Similarly Structure members are
accessed using dot [.] operator. It is called as “Structure member Operator”. Use this Operator
in between “Structure name” & “member name”
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
Syntax:
struct_var.member_name;
Example:
stud1.rno = 01;
strcpy(stud1.name, “Kalam”);
stud1.course = “IT”;
stud1.fees = 45000;
Selecting a member from a structure pointer happens frequently, it has its own operator
-> which acts as follows. Assume that stud1 is a pointer to a structure of type student we would
refer to the name member as
stud1.rno->name
#include<stdio.h>
#include<conio.h>
struct student
int rollno;
char name[80];
float fees;
char DOB[80];
};
int main()
{
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
clrscr( );
scanf(“%d”, &stud1.rollno);
scanf(“%s”, stud1.name);
scanf(“%f”, &stud1.fees);
scanf(“%s”, stud1.DOB);
getch( );
OUTPUT:
NAME. = rahul
2 DISCUSS ABOUT ARRAYS OF STRUCTURES WITH EXAMPLE. Nov 2016, May 2016,
Jan 2016, May 2015, Jan 2013,May-2019)
ARRAYS OF STRUCTURES
An array of structures is the same way as we declare an array of built-in data type. The
array of structures can be used when common structure definition is need for the process of
information.
Syntax
Example
Now, to assign values to the ith student of the class, we will write,
stud[i].r_no = 09;
stud[i].name = “RAM”;
stud[i].course = “CSE”;
stud[i].fees = 60000;
/*program to read and display information of all the students in the class*/(May-
2019)
#include<stdio.h>
#include<conio.h>
struct studentinfo
{
int roll;
char name[20];
int age;
};
void main()
{
clrscr();
int n,i;
printf("\nHow many students information do you want to enter?");
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
scanf("%d",&n);
printf("Enter Student Information:");
for(i=1;i<=n;i++)
{
printf("\nEnter Roll no.:");
scanf("%d",&s[i].roll);
printf("\nEnter the name of the student:");
scanf("%s",&s[i].name);
printf("\nEnter the age of the student:");
scanf("%d",&s[i].age);
}
printf("\n\nInformation of all studenst:");
for(i=1;i<=n;i++)
{
printf("\nRoll no.:%d",s[i].roll);
printf("\nName:%s",s[i].name);
printf("\nAge of student:%d\n\n",s[i].age);
}
getch( );
C permits the use of arrays as structure members. We have already used arrays of
characters inside a structure. Similarly, we can use single-dimensional or multidimensional
arrays of type int or float. For example, the following structure declaration is valid:
struct marks
int number;
float subject[3];
} student[2];
Here the member structure subject contains three elements, subject[0], subject[1],
subject[2]. These elements can be accessed using appropriate subscripts. For example, the
name, student[1].subject[2];
would refer to the marks obtained in the third subject by the second student.
struct marks
int sub[3];
int total;
};
int i,j;
student[i].total += student[i].sub[j];
total.sub[j] += student[i].sub[j];
total.total += student[i].total;
printf("STUDENT TOTAL\n\n");
printf("\nSUBJECT TOTAL\n\n");
Output:
STUDENT TOTAL
Student[1] 193
Student[2] 197
Student[3] 164
SUBJECT TOTAL
Subject-1 177
Subject-2 156
Subject-3 221
[ Univ. Ques. Jan 2016, May 2015, Jan 2015, May 2013]
NESTED STRUCTURES
A structure can be placed within another structure. That is, a structure may contain another
structure as its member. Such a structure that contains another structure as its member is
called a nested structure.
Example:
#include<stdio.h>
#include<conio.h>
struct DOB
int day;
int month;
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
int year;
};
struct student
int roll_no;
char name[100];
float fees;
};
int main( )
clrscr( );
scanf(“%d”, &stud.roll_no);
scanf(“%s”, stud.name);
scanf(“%f”, &stud.fees);
getch( );
}
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
OUTPUT:
NAME. = rahul
Similar to arrays and pointers a structure can also be passed to a function. There are
three ways of passing a structure to a function.
They are;
This method is used to pass each member of the structure as an actual argument of
the function call statement.
When you pass a member of a structure to a function as an argument, you are
actually passing the value of that member to the function.
The arguments are then treated independently as ordinary variables.
For example;
struct employee
int empno;
char empname[20];
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
float salary;
} emp;
employ(emp.empno)
employ(emp.empname)
employ(emp.salary);
Where employ(emp.empno) passes the integer value of empno to the function employ().
This method is the most common method and becomes inefficient when the structure is
large.
The member of a structure can also be passed to function by passing the address of
the members.
In this, a method the address location of the members is passed to the called
function, hence the address operator (&) is used before the structure name.
employ(&emp.salary);
#include<stdio.h>
struct employee
int empno;
char empname[20];
};
main()
scanf(“%d%s”,&emp1.empno,emp1.empname);
employ(emp1);
OUTPUT:
struct stores
{
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
char name[20];
float price;
int quantity;
};
main()
int q_increment;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
printf("Name : %s\n",item.name);
printf("Price : %f\n",item.price);
printf("Quantity : %d\n",item.quantity);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
value = mul(item);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
{
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
product.price += p;
product.quantity += q;
return(product);
return(stock.price * stock.quantity);
Output:
10 12
Name : XYZ
Price : 35.750000
Quantity : 24
Self referential structures are those structures that contain a reference to data of its same
type. That is, a self referential structure in addition to other data contains a pointer to a data
that is of the same type as that of the structure. For example, consider the structure node given
below.
struct node
int item;
};
Here the structure node will contain two types of data- an integer item and next that is a
pointer to a node. Self-referential structure is the foundation of other data structures. They are
most commonly used in linked list data structure implementation
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
Example:
#include <stdio.h>
#include<conio.h>
struct stud
int roll;
char name[30];
int age;
void main()
clrscr( );
n2.next = &n3;
n3.next = NULL;
while (p != NULL)
p = p->next;
getch( );
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
OUTPUT:
101 ram 19
102 raj 19
103 rahul 18
101 ram 19
102 raj 19
103 rahul 18
15
Explanation:
The list consists of three nodes n1, n2 and n3 by declaring struct stud n1, n2, n3. To create
the links between nodes, we write n1.next = &n2; n2.next = &n3; By pointing the address of
the first element, and while loop we can traverse through the nodes and display the student
details.
7: SIZE OF STRUCTURES
We normally use structures, unions and arrays to create variables of large sizes. The
actual size of these variables in terms of bytes may change from machine to machine. We may
use the unary operator sizeof to tell us the size of a structure (or any variable). The expression
sizeof(struct x)
we evaluate the number of bytes required to hold all the members of the structure x. If y is a
simple structure variable of type struct x, then the expression
sizeof(y)
would also give the same answer. However, if y is an array variable of type struct x, then
sizeof(y)
sizeof(y)/sizeof(x)
TYPEDEF
type – data-type
Example: C Program to create user defined data type weeks on int data type
and used it in the program.
#include<stdio.h>
#define D 7
void main
weeks wk;
scanf(“%d”, &wk);
Output:
Enter weeks: 4
Number of days = 28
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
Pointer:
INTRODUCTION
The Pointer is a variable that store the address of the another variable. Pointers can be
used to access and manipulate data stored in the memory. This is known as Pointers.
Syntax:
data_type *Pointer_varaible;
Example:
int *a;
float *b;
char *c;
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
Explanation
int rno=40;
rno
Variable
40
2005 Value
Address
Let as assume that the system has assign the address location as 2005 for a variable ‘sno’.
FEATURES OF POINTERS
Once the pointer is declared and assigned to the address of another variable, the variable
can be accessed through its pointers. This is done by using another unary operator * (asterisk),
usually known as the indirection operator or dereferencing operator.
Example:
int *a;
b=25;
a=&x;
Program:
#include<stdio.h>
#include<conio.h>
main ( )
clrscr();
a=&a;
Output:
Value of a=22
Address of a=4000
Example:
P=&n;
When users declare an array the consecutive memory locations are located to the array
of elements. The elements of an array can be efficiently accessed by using pointers.
Example:
int a[5]={10,20,30,40,50};
10 20 30 40 50 value
The base address of the array starts with 0th element of the array. The array is in integer
type.
The integer will have 2 bytes.
The address of the next address element is incremented by 2.
Program:
#include<stdio.h>
main( )
int i,total,a[5],*c;
for(i=0;i<5;i++)
scanf("%d",&a[i]);
}
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
c=a;
for(i=0;i<5;i++)
total=total+*c;
c=c+1;
printf("\n Total=%d",total);
Output:
Total=150
Example:
int *a;
int *b;
b=a=0;
In general, pointer is a variable that contains the address of another variable. Similarly,
another pointer variable can store the address of this pointer variable. This is a pointer to
pointer variable.
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
Pictorial Representation
Note:
Example:
int a=20;
int *b;
int **c;
b=&a;
c=&b;
Program:
#include<stdio.h>
main()
int **c;
b=&a;
c=&b;
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
printf(“Value of a is %d\n”,a);
printf(“Value of a is %d\n”,*(&a));
printf(“Value of a is %d\n”,*b);
printf(“Value of a is %d\n”,**c);
printf(“Address of a is %u”,&a);
printf(“Address of b is %u”,&b);
printf(“Address of a is %u”,*c);
printf(“Address of b is %u”,&b);
printf(“Address of b is %u”,c);
printf(“Address of c is %u”,&c);
getch();
Output:
Value of a is 20
Value of a is 20
Value of a is 20
Value of a is 20
Address of a is 2005
Address of b is 2005
Address of a is 2005
Address of b is 2005
Address of b is 2005
Address of c is 2005
Syntax:
Sri ManakulaVinayagar Engineering College, Puducherry U23CSTC01 –Programming in C
char *pointer_character;
A character pointer can also be used to access character arrays and string constants, in
the same way as accessing numeric arrays using their respective pointer variable.
Pointer variables can be used in expressions. As a user, we can use integer for addition and
subtraction from pointers.
Example:
Another Example:
The Pointer can be used as an argument in functions. The arguments (or) parameters to the
function are passed in two ways. They are given below.
In the above two method, especially ‘call by reference’ is used to achieve the technique of
‘Pointers and Functions’
The calling function sends the addresses of the variables and the called function must
declare those incoming arguments as pointers.
In order to modify the variables sent by the caller, the called function must dereference
the pointers that were passed to it.
Thus, passing pointers to a function avoid the overhead of copying data from one
function to another.
This Process is otherwise called as Call by reference.
#include<stdio.h>
#include<conio.h>
Pointer which point structure in the same way as used with the other variable are called
Structure pointers.
member1;
member2;
………………
………………
membern;
};
struct tag*ptrvar;
char book_name;
int pages;
float price;
};
struct book_list*ptr;
Example:
#include<stdio.h>
#include<conio.h>
main()
{
struct book
{
char name[25];
char author[25];
int pages;
};
struct book b1={“Computer Programming”, “Kamthane”, 479};
struct book 8ptr;
ptr=&b1;
clrscr();
printf{“\n %s by %s of %d pages”, b1.name, b1.author, b1.pages};
printf{“\n %s by %s of %d pages”, ptr->name, ptr->author, ptr->pages};
OUTPUT:
Computer Programming by Kamthane of 479 pages
Example
#include <stdio.h>
int main()
{
char *cities[] = {"Iran", "Iraq"};
int i;
for(i = 0; i < 2; i++)
printf("%s\n", cities[i]);
return 0;
}
Output
Iran
Iraq