1 Types The type of a variable represents a set of values that may be assigned to the variable. For example, an integer variable is one that may take the.

Slides:



Advertisements
Similar presentations
C Programming Lecture 23 Enumeration Types Structures.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
1 C++ Syntax and Semantics The Development Process.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Multiple-Subscripted Array
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
PRIMITIVE DATA TYPES -Integer -Floating Point -Decimal -Boolean -Character STRINGS -Character Array -Class -String Length -Static -Limited Dynamic -Dynamic.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
C Arrays Systems Programming. Systems Programming: Arrays 22 ArraysArrays  Arrays  Defining and Initializing Arrays  Array Example  Subscript Out-of-Range.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
CISC105 – General Computer Science Class 9 – 07/03/2006.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Pointers Pointers are a unique class of variables whose purpose is to “point” to other variables Pointers allow programmers direct access to memory addresses.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Simple Data Types Built-In and User Defined Chapter 10.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Ch Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
CS 1430: Programming in C++.
1 Lecture06: Complex Types 4/18/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Enumerated Types Mr. Dave Clausen La Cañada High School.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
LESSON 06.
Andy Wang Object Oriented Programming in C++ COP 3330
DATA HANDLING.
C Arrays Systems Programming.
MON TUE WED THU
Chapter 1: Introduction to Data Structures(8M)
2008 Calendar.
Sun Mon Tue Wed Thu Fri Sat
C Structures, Unions, Bit Manipulations and Enumerations
Sun Mon Tue Wed Thu Fri Sat
1/○~1/○ weekly schedule MON TUE WED THU FRI SAT SUN MEMO
A3 1.1c To Multiply & Divide Positive & Negative Numbers
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
Structures Chapter 4.
2008 Calendar.
Presentation transcript:

1 Types The type of a variable represents a set of values that may be assigned to the variable. For example, an integer variable is one that may take the values –2 31 to 2 31 – 1 What if we want a variable that may take values from a non-numerical set? For example, variable day may take values from the set {Mon, Tue, Wed, Thu, Fri, Sat, Sun}

2 Enumerations An enumerated type is a set of values defined by the programmer. Each element of an enumerated type is given a unique name by the programmer. Example: enum {Mon, Tue, Wed, Thu, Fri, Sat, Sun} Each element of an enumerated type is internally represented by an integer. Unless the programmer explicitly specifies the integers, the values are consecutive starting at 0. This allows us to perform iterate from one variable to another.

3 Enumerations In order to be able to declare enumerated variables, an enumeration must be given a name. Example: enum dayT {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; int main () { dayT today, tomorrow; today = Mon; cout << today; // this will print 0 tomorrow = (today + 1) % 7; if (tomorrow == Sat) cout << “It’s the weekend!” << endl;...

4 Enumerations enum dayT {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; Mon, Tue, etc. are called “enumeration constants” CAUTION! When you declare dayT today;, today is essentially an integer. There are no restrictions on its value. Example: today = 135; is allowed (but meaningless)

5 Structures We have seen that we can use arrays when we want several similar variables of the same type. But what if we need to store several pieces of information that have different types but refer to the same entity? Example: a student roster may contain the name and id number of each student. We’d like one variable to somehow contain two components, the name and the id. The answer is a structure: a collection of named components (fields) that may have different types. (Sneak preview: C++ classes are very similar to C structures)

6 Structures Example: struct studentInfoT { char name[30]; int id_number; }; int main () { studentInfoT student1; // a variable of type studentInfoT studentInfoT roster[15]; // an array of 15 elements of type // studentInfoT.

7 Structure To access the individual fields of a structure we use the dot operator. Example 1: struct pointT { double xcoord; double ycoord; }; int main () { pointT origin; origin.xcoord = 0.0; origin.ycoord = 0.0;... }

8 Structure To access the individual fields of a structure we use the dot operator. Example 2: struct studentInfoT { int id_number; char grade; }; int main () { studentInfoT classlist[15]; classlist[0].id = ; classlist[0].grade = ‘A’;... }

9 Pointers A pointer variable is a variable whose value is an address. Some programmers use the terms pointer and address interchangeably. Pointers allow us to manipulate data through the address This makes pointers very useful but also very dangerous.

10 Pointers Example: address variable name, value 0xbfffc290:px, 0xbfffc294 x, -10 px is a pointer variable that contains the address of an integer variable, x. The value of px is address 0xbfffc294 The value of x is integer -10 We say that px points to x Because x is an int, we say that the base type of px is int 0xbfffc294: 0xbfffc298:

11 Pointers A pointer variable is declared as follows: base_type * variable_name ; the type of the object pointed to by the pointer the name of the pointer variable. It follows the same rules as regular variables, and usually starts with a p the asterisk signifies that this is a declaration of a pointer variable

12 Pointers Examples All of these variables are currently unitilialized int *pnum; // pnum will contain the address of an // integer float *ptemp; // ptemp will contain the address of a // floating point number char *pword, letter; // CAUTION! pword is a pointer // to char but letter is a char.

13 Pointers A pointer variable is initialized by assigning an address to it. Example 1: int list[5] = {10, 20, 30, 40, 50}; int *pnum; pnum = list; /* pnum now contains the address where the list begins. In other words, it contains the address of the first element of the list. */

14 Pointers A pointer variable is initialized by assigning an address to it. We can access the address of a variable by using the & operator. int *pnum, number; pnum = &number; /* pnum now points to number */

15 Pointers char *pletter, *pgrade; char grades[4] = {'A', 'B', 'C', 'D'}; char ch; pgrade = grades; /* pgrade points to the beginning of grades */ pletter = &ch; /* now, pletter points to variable ch */ pletter = pgrade; /* now, pletter points to the beginning of grades */ grades = pletter; /*ILLEGAL! The address of an array is constant! You cannot change its value */ pletter = 100; /* ILLEGAL! 100 is an integer, pletter's value should be an address */ pletter = &pgrade /* ILLEGAL! pletter should be the address of a char, but &pgrade is the address of a pointer (or, to be more specific, it's the address of the address of a char). The types do not match. */