C programming structure
C programming structure
Structured LANGUAGE-
and MODULE-4
Object
Oriented By,
Dr. S. Vinila Jinny,
Programming ASP/SCOPE
SYLLABUS
INTRODUCTION
At times, during programming, there is a
need to store multiple logically related
elements under one roof.
For instance, an employee’s details like
name, employee number, and designation
need to be stored together.
In such cases, the C language provides
structures to do the job for us.
WHAT IS STRUCTURE?
The structure is a user-defined data type in C, which
is used to store a collection of different kinds of
data.
struct structName
{
// structure definition
Data_type1 member_name1;
Data_type2 member_name2;
Data_type2 member_name2;
};
DESCRIPTION OF THE SYNTAX
Keyword struct: The keyword struct is used at the
beginning while defining a structure in C.
struct employee
{ int id;
char name[50];
float salary;
};
int main(){
struct employee e1, e2;
return 0; }
DECLARING STRUCTURE VARIABLE
2nd way:
Let's see another way to declare variable at the time of
defining the structure.
struct employee
{ int id;
char name[50];
float salary;
}e1,e2;
ACCESSING MEMBERS OF THE STRUCTURE
int number;
printf("NAME: %s\n", ptr->name);
int rank;
printf("NUMBER: %d\n", ptr->number);
};
printf("RANK: %d", ptr->rank);
return 0;
}
STRUCTURE ASSIGNMENT
Structure variables can be assigned to another structure variable
using assignment operator(=)
#include<stdio.h>
int main()
{
Struct sample
{
int a;
int b;
}x,y;
x.a=10;
x.b=200;
y=x; // direct assignment
printf(“%d %d”, y.a,y.b)
return 0;
}
ARRAY OF STRUCTURES
As normal varaiable, we can create array on
structure variable.
Syntax:
struct structure_name variable[array size];
void ask()
ARRAY OF {
STRUCTURES for(i = 0; i < 3; i++)
{
#include<stdio.h> printf("\nEnter %dst Employee
record:\n", i+1);
struct Employee
printf("\nEmployee name:\t");
{
scanf("%s", emp[i].ename);
char ename[10];
printf("\nEnter Salary:\t");
int sal; scanf("%d", &emp[i].sal);
}; }
struct Employee emp[5]; printf("\nDisplaying Employee record:\n");
int i, j; for(i = 0; i < 3; i++)
{
void main() printf("\nEmployee name is %s",
{ emp[i].ename);
ask(); printf("\nSlary is %d", emp[i].sal);
} }
}
PASSING STRUCTURES TO FUNCTIONS
#include<stdio.h> void show(struct Student st)
struct Student {
{ printf("\nstudent name is %s", st.name);
char name[10]; printf("\nroll is %d", st.roll);
int roll; }
};
void show(struct Student st);
void main()
{
struct Student std;
printf("\nEnter Student record:\n");
printf("\nStudent name:\t");
scanf("%s", std.name);
printf("\nEnter Student rollno.:\t");
scanf("%d", &std.roll);
show(std);
}
STRUCTURES WITHIN STRUCTURES
Nesting of structures, is also permitted in C language.
Nested structures means, that one structure has another
stucture as member variable.
struct complex
{
int imag;
float real;
};
struct number
{
struct complex comp;
int integers;
} nu;
nu.comp.imag = 11;
ARRAYS WITHIN STRUCTURES
A structure can have array as its member.
struct Student
{
int a[10][10];
float b;
}y;
POINTERS AND STRUCTURES
_COMPLEX a, b, c;
30
TYPEDEF : AN EXAMPLE
typedef struct{
float real;
float imag;
} _COMPLEX;
tmp = a;
a = b;
b = tmp;
}
TYPEDEF : AN EXAMPLE
void print (_COMPLEX a)
{
printf("(%f, %f) \n",a.real,a.imag);
}
void main()
{
_COMPLEX x={4.0,5.0}, y={10.0,15.0};
print(x); print(y);
swap(x,y);
print(x); print(y);
}
swap.c
TYPEDEF : AN EXAMPLE
Note: typedef is not restricted to just
structures, can define new types from any
existing type
Example:
typedef int INTEGER
Defines a new type named INTEGER from the
known type int
Can now define variables of type INTEGER which
will have all properties of the int type
INTEGER a, b, c;
STRUCTURES AND FUNCTIONS
A structure can be passed as argument to a
function
A function can also return a structure
EXAMPLE: COMPLEX NUMBER
ADDITION
void main()
{
_COMPLEX a, b, c;
scanf(“%f %f”, &a.real, &a.imag);
scanf(“%f %f”, &b.real, &b.imag);
c = add (a, b) ;
printf(“\n %f %f”, c,real, c.imag);
}
_COMPLEX add(_COMPLEX x, _COMPLEX
y)
{
_COMPLEX t;