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

C programming structure

The document provides an overview of structures in C programming, detailing their definition, syntax, and usage for storing logically related data types. It covers how to declare structure variables, access their members, and use pointers with structures, along with examples and exercises for practice. Additionally, it explains typedef for creating new data types and the ability to pass structures to functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C programming structure

The document provides an overview of structures in C programming, detailing their definition, syntax, and usage for storing logically related data types. It covers how to declare structure variables, access their members, and use pointers with structures, along with examples and exercises for practice. Additionally, it explains typedef for creating new data types and the ability to pass structures to functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

BCSE102L C PROGRAMMING

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.

 The structure is something similar to an array; the


only difference is array is used to store the same data
types.

 struct keyword is used to declare the structure in C.

 Variables inside the structure are called members of


the structure.
STRUCTURE DEFINITION
STRUCTURE EXPLANATION
A structure can be defined as a single entity
holding variables of different data
types that are logically related to each
other.
 All the data members inside a structure are
accessible to the functions defined outside
the structure. To access the data members
in the main function, you need to create a
structure variable.
SYNTAX TO DEFINE A STRUCTURE IN C

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.

 structName: This is the name of the structure which is


specified after the keyword struct.

 data_Type: The data type indicates the type of the data


members of the structure. A structure can have data
members of different data types.

 member_name: This is the name of the data member of


the structure. Any number of data members can be
defined inside a structure. Each data member is
allocated a separate space in the memory.
EXAMPLE
struct employee
{ int id;
char name[10];
float salary;
};
EXAMPLE-MEMORY REPRESENTATION
DECLARING STRUCTURE VARIABLE

 We can declare a variable for the structure


so that we can access the member of the
structure easily.
 There are two ways to declare structure
variable:
 By struct keyword within main() function
 By declaring a variable at the time of
defining the structure.
DECLARING STRUCTURE VARIABLE
1st way:
Let's see the example to declare the structure variable by
struct keyword. It should be declared within the main
function.

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

 There are two ways to access structure


members:
 By . (member or dot operator)
 By -> (structure pointer operator)
EXAMPLE
#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
}e1;
int main( )
{
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
return 0;
}
employee 1 id : 101
employee 1 name : Sonoo Jaiswal
STRUCTURE POINTERS
 Structure pointers are pointer variables that
holds the address of another variable.
Syntax:
struct structure_name *ptr;

To access members of structure using the


structure variable, we used the dot . operator.
But when we have a pointer of structure type, we
use arrow -> to access structure members.
STRUCTURE POINTERS

#include <stdio.h> int main()


{
struct my_structure struct my_structure variable = {“JACKIN", 35, 1};
{ struct my_structure *ptr;
char name[20]; ptr = &variable;

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

 You may recall that the name of an array


stands for the address of its zero-th
element.
 Also true for the names of arrays of structure
variables.
 Consider the declaration:
struct stud {
int roll;
char dept_code[25];
float cgpa;
} class[100], *ptr ;
POINTERS AND STRUCTURES
 The name class represents the address of the
zero-th element of the structure array.
 ptr is a pointer to data objects of the type
struct stud.
 The assignment
ptr = class ;
 will assign the address of class[0] to ptr.
 When the pointer ptr is incremented by one
(ptr++) :
 The value of ptr is actually increased by
sizeof(stud).
 It is made to point to the next record.
POINTERS AND STRUCTURES
 Once
ptr points to a structure variable, the
members can be accessed as:
ptr –> roll ;
ptr –> dept_code ;
ptr –> cgpa ;

 The symbol “–>” is called the arrow operator.


WARNING
 When using structure pointers, we should take
care of operator precedence.
 Member operator “.” has higher precedence than “*”.
 ptr –> roll and (*ptr).roll mean the same thing.
 *ptr.roll will lead to error.

 The operator “–>” enjoys the highest priority among


operators.
 ++ptr –> roll will increment roll, not ptr.
 (++ptr) –> roll will do the intended thing.
EXAMPLE: COMPLEX NUMBER ADDITION
void main()
{
struct complex
{
float real;
float cmplex;
} a, b, c;

scanf (“%f %f”, &a.real, &a.cmplex);


scanf (“%f %f”, &b.real, &b.cmplex);

c.real = a.real + b.real;


c.cmplex = a.cmplex + b.cmplex;
printf (“\n %f + %f j”, c.real, c.cmplex);
}
DEFINING DATA TYPE: USING
TYPEDEF
 One may define a structure data-type with a single
name
typedef struct newtype {
member-variable1;
member-variable2;
.
member-variableN;
} mytype;
 mytype is the name of the new data-type
 Also called an alias for struct newtype
 Writing the tag name newtype is optional, can be
skipped
 Naming follows rules of variable naming
29
TYPEDEF : AN EXAMPLE
typedef struct {
float real;
float imag;
} _COMPLEX;

 Defineda new data type named _COMPLEX.


 Now can declare and use variables of this type

_COMPLEX a, b, c;

30
TYPEDEF : AN EXAMPLE
typedef struct{
float real;
float imag;
} _COMPLEX;

void swap (_COMPLEX a, _COMPLEX b)


{
_COMPLEX tmp;

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;

t.real = x.real + y.real;


t.imag = x.imag + y.imag ;
return (t) ;
} 35
EXERCISE PROBLEMS
1. Extend the complex number program to include
functions for addition, subtraction, multiplication, and
division
2. Define a structure for representing a point in two-
dimensional Cartesian co-ordinate system
• Write a function to compute the distance between
two given points
• Write a function to compute the middle point of the
line segment joining two given points
• Write a function to compute the area of a triangle,
given the co-ordinates of its three vertices

You might also like