DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College

Slides:



Advertisements
Similar presentations
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
Advertisements

C Language.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
Multiple-Subscripted Array
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Chapter 8 Arrays and Strings
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
1 Structures. 2 C gives you several ways to create a custom data type. –The structure, which is a grouping of variables under one name and is called an.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
Chapter 8 Arrays and Strings
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Object-Oriented Programming in C++
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Variables and memory addresses
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 CS161 Introduction to Computer Science Topic #15.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
Java Programming Language Lecture27- An Introduction.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
CS1010 Programming Methodology
Pointers, Enum, and Structures
Pointers and Pointer-Based Strings
Student Book An Introduction
DATA HANDLING.
Review for Final Exam.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Review for Final Exam.
Pointers and Pointer-Based Strings
Structures.
Structures, Unions, and Enumerations
Presentation transcript:

DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College

The ability to create your own data types is a powerful feature of C++. You can create your own data type using structures, union and class. In C++, structures and unions have both object-oriented and non-object-oriented attributes. This chapter discusses only their non-object-oriented features.

Structures In C++, a structure is a collection of variables that are referenced under one name. Structures are called aggregate data types because they consist of several different, yet logically connected, variables. Before a structure object can be created, the form of the structure must be defined. This is accomplished by means of a structure declaration. The variables that comprise the structure are called members (also called elements or fields) of the structure.

Structures(2) Generally, all members of the structure will be logically related to each other. We will define a structure that can hold the information relating to a company's inventory. An inventory record typically consists of several pieces of information. The following code fragment declares a structure that defines the item name, cost and retail price, number on hand, and resupply time for maintaining an inventory.

Structures(3) The keyword struct tells the compiler that a structure declaration is beginning. struct inv_type { char item[40]; // name of item double cost; // cost double retail; // retail price int on_hand; // amount on hand int lead_time; // number of days before resupply };

Structures(4) In the preceding declaration, no variable has actually been created. To declare an actual variable (i.e., a physical object) with this structure, you would write something like this: inv_type inv_var; You can also declare one or more variables at the same time that you define a structure, as shown here: struct inv_type { char item[40]; // name of item : } inv_varA, inv_varB, inv_varC;

Structures(5) It is important to understand that each structure variable contains its own copies of the structure’s members.

Accessing Structure Members Individual structure members are accessed through the use of a period (generally called the "dot" operator). The following code will assign the value to the cost field of the structure variable inv_var, declared earlier. inv_var.cost = 10.39; Therefore, to print cost on the screen, you could write cout << inv_var.cost;

Accessing Structure Members(2) In the same fashion, the character array inv_var.item can be used to call gets( ), as shown here: gets(inv_var.item); If you want to access the individual elements of the array inv_var.item, you can index item. int t; for(t=0; inv_var.item[t]; t++) cout << inv_var.item[t];

Arrays of Structures Structures may be arrayed. To declare a 100- element array of structures of type inv_type (defined earlier), you would write inv_type invtry[100]; To access a specific structure within an array of structures, you must index the structure name. To display the on_hand member of the third structure, you would write cout << invtry[2].on_hand;

Passing Structures to Functions When a structure is used as an argument to a function, the entire structure is passed by using the standard call-by-value parameter passing mechanism. When using a structure as a parameter, remember that the type of the argument must match the type of the parameter. For example, the following program declares a structure called sample, and then a function called f1( ) that takes a parameter of type sample.

Passing Structures to Functions(2) // Pass a structure to a function. #include using namespace std; // define a structure type struct sample { int a; char ch; } ; void f1(sample parm); int main() { struct sample arg; // declare arg arg.a = 1000; arg.ch = 'X'; f1(arg); return 0; } void f1(sample parm) { cout << parm.a << " " << parm.ch << "\n"; } Here, both arg in main( ) and parm in f1( ) are of the same type. Thus, arg can be passed to f1( ).

Assigning Structures You can assign the contents of one structure to another as long as both structures are of the same type. // Demonstrate structure assignments. #include using namespace std; struct stype { int a, b; }; int main() { stype svar1, svar2; svar1.a = svar1.b = 10; svar2.a = svar2.b = 20; svar1.a=10; svar1.b=10; svar2.a=20; svar2.b=20;

cout << "Structures before assignment.\n"; cout << "svar1: " << svar1.a << ' ' << svar1.b; cout << '\n'; cout << "svar2: " << svar2.a << ' ' << svar2.b; cout << "\n\n"; svar2 = svar1; // assign structures cout << "Structures after assignment.\n"; cout << "svar1: " << svar1.a << ' ' << svar1.b; cout << '\n'; cout << "svar2: " << svar2.a << ' ' << svar2.b; return 0; }

This program displays the following output: Structures before assignment. svar1: svar2: Structures after assignment. svar1: svar2: 10 10

Pointers to Structures and the Arrow Operator C++ allows pointers to structures in the same way that it allows pointers to any other type of variable. You declare a structure pointer as you would any other pointer variable, by putting an * in front of a structure variable's name. inv_type *inv_pointer; declares inv_pointer to be a pointer to data of that type: To find the address of a structure variable, you must place the & operator before the structure variable's name.

Pointers to Structures and the Arrow Operator(2) struct bal { float balance; char name[80]; } person; bal *p; // declare a structure pointer then p = &person; puts the address of person into the pointer p. The members of a structure can be accessed through a pointer to the structure.

Pointers to Structures and the Arrow Operator(3) You must use the –> operator to accesses balance through p: p->balance

Exercise Exercise on C primer Plus 5 th edition. Chapter What’s wrong with this template? structure { char itable; Int num[20]; Char * togs }

Exercise(2) 2. #include struct house float sqft; Int rooms; Int stories; Char address[40]; }; Int main(void) Struct house fruzt={1560.0,6,1,”22 Spiffo Road”); Struct house *sign; Sigh = &fruzt;

cout stories<<endl; cout<<fruzt.address<<endl; cout address[3]<<fruzt.address[4]<<endl;

sqftroomsstoriesaddress spiffo Road fruzt *sign Output??

Summary A C++ structure provide the means to store several data items, usually of different types, int the same object (name). The membership dot (.) operator enables you to access the individual members of a structures. If you have a pointer to a structure, you can use the pointer and the indirect membership ( arrow operator (->) instead of a name and the dot operator to access individual members.

Summary(2) The typedef facility enables you to create a group of symbolic integer constants (enumeration constant) and to define an associated enumeration type.