3. //Structures //Collection of various types of variables
#include <stdio.h>
struct student //declares a structure
{
int rollnumber;
int semnumber;
double age_years;
}; // See the use of semicolon
int main()
{
struct student student1; /*defines a variable student1 of type structure*/
student1.rollnumber = 15;
student1.semnumber = 1;
student1.age_years = 18.5;
//Print the structure members
printf("Roll Nomber = %dn",student1.rollnumber);
printf("Semesetr Number = %dn",student1.semnumber);
printf("Age = %lfn",student1.age_years);
getchar(); return 0; }
Dr. Yousaf, PIEAS
4. • They make your programming easier.
• You can extend the language - for example, if you use
complex numbers a lot then you can write structures
and functions which deal with complex numbers.
• Structures are C’s way of grouping collections of
data into a single manageable unit.
– This is also the fundamental element of C upon which
most of C++ is built (i.e., classes).
– Similar to Java's classes.
Dr. Yousaf, PIEAS
Structures
5. Structures
• struct is used to describe a new data type.
• int, double and char are types of variables.
With struct you can create your own. It is a
new way to extend the C programming language.
• If programmers do a good job of this, the end
user doesn't even have to know what is in the
data type.
Dr. Yousaf, PIEAS
6. Structures
• An example:
– Defining a structure type:
struct student //declares a structure
{
int rollnumber;
int semnumber;
double age_years;
};
• This defines a new type struct student.
• No variable is actually declared or generated.
Dr. Yousaf, PIEAS
7. #include <stdio.h>
//group things together
struct database
{
int id_number;
int age;
float salary;
};
int main()
{
struct database employee;
employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.21;
}
Structures
Dr. Yousaf, PIEAS
8. #include <stdio.h>
//group things together
struct database
{
int id_number;
int age;
float salary;
};
int main()
{
struct database employee;
employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.21;
}
Structures
Dr. Yousaf, PIEAS
• Members of the structure
• No space in memory
• Accessing the members of
structure using Dot operator
9. // Program using Concept of
Structures
#include <stdio.h>
struct student //declares a structure
{
int rollnumber;
int semnumber;
double age_years;
}; // See the use of semicolon
int main()
{
struct student student1;
/*defines a variable student1 of
type structure */
struct student student2;
student1.rollnumber = 15;
student1.semnumber = 1;
student1.age_years = 18.5;
//Print the structure members
printf("Roll Nomber =
%dn",student1.rollnumber);
printf("Semesetr Number =
%dn",student1.semnumber);
printf("Age = %lfn",
student1.age_years);
student2.rollnumber = 22;
student2.semnumber = 1;
//Print the structure members
printf("nnRoll Nomber =
%dn",student2.rollnumber);
printf("Semesetr Number =
%dn",student2.semnumber);
getchar(); return 0;
}
Dr. Yousaf, PIEAS
10. /*More features of structures
Initialization of member variables
Program using Concept of
Structures*/
#include <stdio.h>
struct student //declares a structure
{
int rollnumber;
int semnumber;
double age_years;
}; // See the use of semicolon
int main()
{
struct student student1 =
{15,1,18.5}; /*defines a variable
student1 of type structure */
struct student student2;
//Print the structure members
printf("Roll Nomber =
%dn",student1.rollnumber);
printf("Semesetr Number =
%dn",student1.semnumber);
printf("Age =
%lfn",student1.age_years);
student2 = student1;
//Print the structure members
printf("nnRoll Nomber =
%dn",student2.rollnumber);
printf("Semesetr Number =
%dn",student2.semnumber);
printf("Age =
%lfn",student1.age_years);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
11. /*More features of structures
Initialization of member variables
Program using Concept of
Structures*/
#include <stdio.h>
struct student //declares a structure
{
int rollnumber;
int semnumber;
double age_years;
} student1, student2;
int main()
{
student1 = {15,1,18.5}; /*defines
a variable student1 of type
structure */
//Print the structure members
printf("Roll Nomber =
%dn",student1.rollnumber);
printf("Semesetr Number =
%dn",student1.semnumber);
printf("Age =
%lfn",student1.age_years);
student2 = student1;
//Print the structure members
printf("nnRoll Nomber =
%dn",student2.rollnumber);
printf("Semesetr Number =
%dn",student2.semnumber);
printf("Age =
%lfn",student1.age_years);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
12. • Define struct variables:
struct coord
{
int x,y ;
} first, second;
• Another Approach:
struct coord
{
int x,y ;
};
...............
struct coord first, second, third; /* declare variables */
Dr. Yousaf, PIEAS
Structures
13. • Access structure variables by the dot (.) operator
• Generic form:
structure_var.member_name
• For example:
first.x = 50 ;
second.y = 100;
• These member names are like the public data members
of a class in Java (or C++).
– No equivalent to function members/methods.
• struct_var.member_name can be used anywhere a
variable can be used:
– printf ("%d , %d", second.x , second.y );
– scanf("%d, %d", &first.x, &first.y);
Dr. Yousaf, PIEAS
Structures
14. • You can assign structures as a unit with =
first = second;
instead of writing:
first.x = second.x ;
first.y = second.y ;
• Although the saving here is not great
– It will reduce the likelihood of errors and
– Is more convenient with large structures
• This is different from Java where variables are simply
references to objects.
first = second;
makes first and second refer to the same object.
Dr. Yousaf, PIEAS
Structures
15. /* Addition of lengths using
concept of structures*/
#include <stdio.h>
struct AddLength
{
int feet;
float inches;
}; // See the use of semicolon
int main()
{
struct AddLength d1, d2, d3;
int carry;
puts("This program adds two
values of lengths");
puts("Enter the value of length in
feet and inches");
puts("Enter the value of feet
for first length");
scanf("%d", &d1.feet);
puts("Enter the value of inches
for first length");
scanf("%f", &d1.inches);
puts("Enter the value of feet
for second length");
scanf("%d", &d2.feet);
puts("Enter the value of inches
for second length");
scanf("%f", &d2.inches);
// continued to next slide
Dr. Yousaf, PIEAS
17. • C program to add, subtract, multiply and divide
Complex Numbers
• C Program to add, subtract, multiply and divide
complex numbers :- This program performs basic
operations on complex numbers i.e addition,
subtraction, multiplication and division. This is a
menu driven program in which user will have to
enter his/her choice to perform an operation. User
can perform operations until desired. To easily
handle a complex number a structure named
complex has been used, which consists of two
integers first integer is for real part of a complex
number and second for imaginary part.
Dr. Yousaf, PIEAS
18. // C programming code for addition of
complex numbers
#include<stdio.h>
#include<stdlib.h>
struct complex
{
int real, img;
};
int main()
{
struct complex c1, c2, c3;
printf("Enter a and b where a + ib is the
first complex number.");
printf("na = ");
scanf("%d", &c1.real);
printf("b = ");
scanf("%d", &c1.img);
printf("Enter c and d where c + id is
the second complex number.");
printf("nc = ");
scanf("%d", &c2.real);
printf("d = ");
scanf("%d", &c2.img);
c3.real = c1.real + c2.real;
c3.img = c1.img + c2.img;
if ( c3.img >= 0 )
printf("Sum of two complex numbers
= %d + %di",c3.real,c3.img);
else
printf("Sum of two complex numbers
= %d %di",c3.real,c3.img);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
19. Solve a Problem
To represent a complex number, write a structure
complexNum , decide the members of the structure
and their type, create two variables of type
complexNum and initialize them from user input,
check whether the number are equal or not?
Dr. Yousaf, PIEAS
20. What else can we do with structs?
We can pass and return structs to/from
functions. But make sure the function prototype
is _AFTER_ the struct is declared.
Dr. Yousaf, PIEAS
21. /*Multiplication of complex numbers through function
using the concept of structures*/
struct complex
{
float imag;
float real;
};
complex mult_complex (complex no1, complex no2);
int main()
{
/* Main goes here */
}
// Definition of function
complex mult_complex (complex no1, complex no2)
{
struct complex answer;
answer.real= no1.real*no2.real - no1.imag*no2.imag;
answer.imag= no1.imag*no2.real + no1.real*no2.imag;
return answer;
} Dr. Yousaf, PIEAS
22. /*Multiplication of complex numbers through function
using the concept of structures*/
typedef struct complex
{
float imag;
float real;
} CPLX;
CPLX mult_complex (CPLX, CPLX);
int main()
{
CPLX n1,n2;
/* Main goes here */
}
// Definition of function
CPLX mult_complex (CPLX no1, CPLX no2)
{
CPLX answer;
answer.real= no1.real*no2.real - no1.imag*no2.imag;
answer.imag= no1.imag*no2.real + no1.real*no2.imag;
return answer;}
Dr. Yousaf, PIEAS
The typedef keyword is
used to create a new name
for an existing data type. In
fact, typedef creates a
synonym.
When typedef is used in
structure definition, then
don’t write struct for
variables declaration.
23. /*Multiplication of complex numbers through function
using the concept of structures*/
typedef struct // even no name here also works
{
float imag;
float real;
} CPLX;
CPLX mult_complex (CPLX, CPLX);
int main()
{
CPLX n1,n2;
/* Main goes here */
}
CPLX mult_complex (CPLX no1, CPLX no2)
{
CPLX answer;
answer.real= no1.real*no2.real - no1.imag*no2.imag;
answer.imag= no1.imag*no2.real + no1.real*no2.imag;
return answer; Dr. Yousaf, PIEAS
24. Structures Containing Arrays
• Arrays within structures are the same as any other
member element.
• For example:
struct record
{
float x;
char y [5] ;
} ;
Dr. Yousaf, PIEAS
25. An Example
#include <stdio.h>
struct data
{
float amount;
char fname[30];
char lname[30];
} rec;
int main ()
{
struct data rec;
printf ("Enter the donor's first and last names, n");
printf ("separated by a space: ");
scanf ("%s %s", rec.fname, rec.lname);
printf ("nEnter the donation amount: ");
scanf ("%f", &rec.amount);
printf ("nDonor %s %s gave $%.2f.n", rec.fname,rec.lname,rec.amount);
}
Dr. Yousaf, PIEAS
27. Arrays of Structures
• The converse of a structure with arrays:
• Example:
struct entry
{
char fname [10] ;
char lname [12] ;
char phone [8] ;
} ;
struct entry list [1000];
• This creates a list of 1000 identical entry(s).
• Assignments:
list [1] = list [6];
strcpy (list[1].phone, list[6].phone);
list[6].phone[1] = list[3].phone[4] ;
Dr. Yousaf, PIEAS
28. Initializing Structures
• Simple example:
struct sale
{
char customer [20] ;
char item [20] ;
int amount ;
};
struct sale mysale = { “Mr. XYZ", “Biscuits", 10000 } ;
Dr. Yousaf, PIEAS