Tải bản đầy đủ (.ppt) (23 trang)

Session 11 Introduction to Programming

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (185.4 KB, 23 trang )

LBC, Session 11
Advanced Data types

FPT APTECH COMPUTER EDUCATION HANOI


Objectives












Explain structures and their use
Define structures
Declare structure variables
Explain how structure elements are accessed
Explain how structures are initialized
Explain how assignment statements are used with
structures
Explain how structures can be passed as arguments to
functions
Use arrays of structures
Explain the initialization of structure arrays
Explain how structure pointers can be passed as


arguments to functions
Explain the typedef keyword
LBC/Session 11

2


Structures
• A structure consists of a number of data items, which need
not be of the same data type, grouped together
• The structure could hold as many of these items as desired
1

I
I L L U S I O N B A C H
1
Variable L
L
U
Name of the book
Author
Edition
S
I
O
N Array
LBC/Session 11

3



Defining a Structure
• Forms a template for creating structure variables
• The variables in the structure are called structure
elements or structure members
• Example:
struct cat
{
char bk_name [25];
char author [20];
int edn;
float price;
};

LBC/Session 11

4


Declaring Structure Variables
• Once the structure has been defined, one or
more variables of that type can be declared
• Two ways of declaring struct variables.
 Right after the close bracket of struct declaration.
 Separated from the struct declaration.

LBC/Session 11

5



Declaring Structure Variables (cont.)
struct cat {
char bk_name[25];
char author[20];
int edn;
float price;
} books1, books2;

struct cat {
char bk_name[25];
char author[20];
int edn;
float price;
};

struct cat books1, books2;
Or:
struct cat books1;
struct cat books2;
LBC/Session 11

6


Accessing Structure Elements
 Structure elements are referenced through the use of the
dot operator (.), also known as the membership
operator
 Syntax:


structure_name.element_name
 Example:

scanf(“%s”, books1.bk_name);

LBC/Session 11

7


Initializing Structures
• Can be initialized at the time of
declaration
struct employee
{
int no;
char name [20];
};

• Variables emp1 and emp2 of the type
employee can be declared and
initialized as:
struct employee emp1 = {346, “Abraham”};
struct employee emp2 = {347, “John”};

LBC/Session 11

8



Assignment Statements Used
with Structures-1
• Possible to assign the values of one structure variable to
another variable of the same type using a simple
assignment statement
• If var1 and var2 are structure variables of the same type,
the following statement is valid

var2= var1;

LBC/Session 11

9


Assignment Statements Used
with structures - 2
• In cases where direct assignment is not possible, the
built-in function memcpy() can be used
• Syntax:
memcpy (char * destn, char &source, int nbytes);

• Example:
memcpy (&books2, &books1, sizeof(struct cat));

LBC/Session 11

10



Structures within Structures
 Possible to have one structure within another structure. A structure
cannot be nested within itself
struct issue
{
char borrower [20];
char dt_of_issue[8];
struct cat books;
}issl;
 The way to access the elements of this structure is similar to the one
applied for normal structures,
issl.borrower
 To access elements of the structure cat, a part of structure issue,
issl.books.author
LBC/Session 11

11


Passing Structures as Arguments

• Parameters of a function can be structures.
• This facility is used to pass groups of logically
related data items together instead of passing
them one by one
• The type of the argument should match the type
of the parameter

LBC/Session 11


12


Array of Structures
• A common use of structures is in arrays of structures
• A structure is first defined, and then an array variable of
that type is declared
• Example:
struct cat books[50];
• To the access the variable named author of the fourth
element of the array books:
books[4].author

LBC/Session 11

13


Initialization of Structure Arrays
• Structure arrays are initialized by enclosing the list of
values of its elements within a pair of braces
• Example:
struct unit
{
char ch;
int i;
};
struct unit
{

{'a',
{'b',
{'c',
};

series[3] =
100}
200}
300}
LBC/Session 11

14


Example of structure arrays
struct strucintcal
{
char name[20];
int numb;
float amt;
};
void intcal(struct strucintcal);
void main()
{
struct strucintcal customers[50];
int i;

LBC/Session 11

15



Example of structure arrays (cont.)
/* Accepts data into the structure */
for (i=0; i<50; i++)
{
printf("\nEnter Customer name: ");
gets(customers[i].name);
printf("\nEnter Customer number: ");
scanf("%d", &customers[i].numb);
printf("\nEnter Principal amount: ");
scanf("%f", & customers[i].amt);
intcal(customers[i]);
}
getch();
}
LBC/Session 11

16


Example of structure arrays (cont.)
void intcal(struct strucintcal abc)
{
float si, rate = 5.5, yrs = 2.5;
/* Computes the interest */
si = (abc.amt * rate * yrs) / 100;
printf ("\nThe customer name is %s", abc.name);
printf("\nThe customer number is %d", abc.numb);
printf("\nThe amount is %f", abc.amt);

printf("\nThe interest is %f", si);
return;
}
LBC/Session 11

17


Pointers to Structures
• Structure pointers are declared by placing an asterisk(*)
in front of the structure variable’s name
• The -> operator is used to access the elements of a
structure using a pointer
• Example:
struct cat *ptr_bk;
ptr_bk = &books;
printf(“%s”, ptr_bk->author);
• Structure pointers passed as arguments to functions
enable the functions to modify the structure elements
directly
LBC/Session 11

18


Example of Pointers to Structures
struct strucintcal
{
char name[20];
int numb;

float amt;
};
void intcal(struct strucintcal);
void main()
{
struct strucintcal *ptr_customers;
int i, n;
LBC/Session 11

19


Example of Pointers to Structures (cont.)
printf("\nEnter the number of customers: ");
scanf(“%d”,&n);
ptr_customers=(struct structintcal *) malloc(n
*sizeof(struct strucintcal));
clrscr();
/* Accepts data into the structure */
for (i=0; i<50; i++)
{
printf("\nEnter Customer name: ");
gets((ptr_customers+i)->name);
LBC/Session 11

20


Example of Pointers to Structures (cont.)
printf("\nEnter Customer number: ");

scanf("%d", ptr_customers+i)->numb);
printf("\nEnter Principal amount: ");
scanf("%f", ptr_customers+i)-> amt);
intcal(*(ptr_customers+i));
}
getch();
}
void intcal(struct strucintcal){}
LBC/Session 11

//see slide 16
21


The typedef keyword
• A new data type name can be defined by using the
keyword typedef
• Does not create a new data type, but defines a new
name for an existing type
• Syntax:
typedef type name;
• typedef cannot be used with storage classes

LBC/Session 11

22


Example of typedef
typedef float deci;

typedef deci point;
void main()
{
float num1;
deci num2;
point sum;
printf("\nEnter the values of num1 and num2: ");
scanf(“%f%f”,&num1, &num2);
sum= num1 + num2;
printf("\n The summary of num1 and num2 is: %f“, sum);
getch();
}

LBC/Session 11

23



×