Assignment 1 Background Advanced OOP CS 440/540 Spring 2015 Kenneth Chiu.

Slides:



Advertisements
Similar presentations
Chapter 2: Basic Elements of C++
Advertisements

Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Structures Spring 2013Programming and Data Structure1.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
C Slides and captured lecture (video and sound) are available at:
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.
Week 9 Part 2 Kyle Dewey. Overview Announcement More with structs and memory Assertions Exam #2 Course review.
The foreach LooptMyn1 The foreach Loop The foreach loop gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Introduction to Data Structures Systems Programming.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Pointer Data Type and Pointer Variables II By: Nouf Aljaffan Edited by : Nouf Almunyif.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
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.
Dynamic memory allocation and Pointers Lecture 4.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Data Types Declarations Expressions Data storage C++ Basics.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Integer Types short month; // half of a machine word int car; // one machine word unsigned long distance; Character Types char c = ‘\0’; // one byte char.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
CSC Programming for Science Lecture 36: Structures.
Pointers *, &, array similarities, functions, sizeof.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Charles Clute Tom Most Michael Hein. Strings in C  There is no String... But there’s hope! Strings are character arrays char volume[6]; char volume[6]
String as Arrays, Array Sorting C++ Programming Technologies.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
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.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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 *
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Chapter 5 Murach's JavaScript and jQuery, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Sorts, CompareTo Method and Strings
Pointers and Dynamic Arrays
Chapter 8 Arrays, Strings and Pointers
Assignment 1 Background
C Programming Tutorial – Part I
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Pointer Data Type and Pointer Variables II
C Basics.
Programming Languages and Paradigms
Chapter 15 Pointers, Dynamic Data, and Reference Types
The C Language: Intro.
CS 144 Advanced C++ Programming February 12 Class Meeting
CS31 Discussion 1H Fall18: week 6
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Assignment 1 Background Advanced OOP CS 440/540 Spring 2015 Kenneth Chiu

struct s struct A { char c; double x; int array[1]; }; Can you do this? – A a1, a2; … a1 = a2; // Assignment. if (a1 == a2) { … }; // Equality.

Function Pointers Syntax is like this: – int foo(double x); int (*fp)(double x) = foo; i = (*fp)(3.14); Can reassign: – int foo2(double x); int foo3(double x); int foo4(int x); fp = foo2; fp = foo3; fp = 0; fp = nullptr; // C++11 fp = foo4; Can be combined with structs to look like methods: – struct Foo {... int (*foo2)(Foo *, double x); };... // Assuming that the function pointer has been // properly initialized, it can be used as below. f->foo2(f, 3.14); – Why is f being passed to foo2() ?

String Literal Concatenation Any problems with the below? – printf(“A long, long, long, long,” “long, long sentence.\n”); Two string literals right next to each other are automatically concatenated at compile-time. Handy for long strings.

Macros Can be used for token replacement: – #define PI area = PI*r*r; – #define GREETING “Hola!” printf(“%s\n”, GREETING); printf(GREETING); printf(“\n”); – #define FOREVER while (1) { FOREVER printf(“Stuck in a loop...\n”); } – #define TIMES * #define GETS = a GETS 3 TIMES 2; Can have parameters: – #define alloc(type) (type *) malloc(sizeof(type)) struct Foo *f = alloc(Foo); – #define SQR(x) x*x area = PI*SQR(r); a2 = PI*SQR(2+5); x = 3; x2 = SQR(x++);

Consider: – #define M(t) \ int foo_##t() { \ printf(“%s\n”, “A_” #t); \ } What does M(int) do? – The backslash is a continuation character for long macros. – The ## splices two preprocessor tokens together into one. – The # operator stringifies the following token by putting “” around it. – Recall that two string literals that are right next to each other are automatically joined by the compiler into one string literal. Result is: – int foo_int() { printf(“%s\n”, “A_int”); }

Containers and Iterators A container holds values. An iterator is essentially a “pointer” (or “cursor”) into a container. Every container has a way to obtain an iterator pointing to the first element. Every container has a special iterator value, the “end” value. It represents one past the last element of the container. What happens if you decrement the “begin” iterator? How about increment the “end” iterator?

Example (assume std ): – list l1; l1.push_back(1); l1.push_back(2); for (list ::iterator it = l1.begin(); it != l1.end(); ++it) { cout << *it << endl; } – list l2; l2.push_back(new MyClass(“A”)); l2.push_back(new MyClass(“B”)); for (list ::iterator it = l2.begin(); it != l2.end(); ++it) { cout << *it << endl; }

– // C++11 list l1{1, 2}; for (auto it = l1.begin(); it != l1.end(); ++it) { cout l2{new MyClass(“A”), new MyClass(“B”)}; for (auto p: l2) { cout << p << endl; }

Why Doesn’t End Iterator in the Assignment Point to the Last Element? Glib answer: That’s the way the C++ standard library does it. What if the end iterator pointed to the last element instead? – for (auto it = l.begin(); ???; ++it) { //... } – Empty list?

Why Doesn’t End Iterator in the Assignment Point to the Last Element? Glib answer: That’s the way the C++ standard library does it. Keep in mind: – Iterators in C++ are modeled after pointers. – Idiomatic iteration over arrays in C/C++: for (int i = 0; i < n; i++) { a[i] = … ; } – Common way to iterate using pointers: int *begin = new int[n], *end = begin + n; for (int *p = begin; p < end; p++) { *p = …; } But we could do: – for (int i = 0; i <= n - 1; i++) { a[i] = …; } – int *begin = new int[n], *end = begin + n – 1; for (int *p = begin; p <= end; p++) { *p = …; }

To iterate over a region of length n in the middle: – for (int i = pos; i < pos + n; i++) { … } – for (int *p = pos; p < pos + n; p++) { … } – for (int i = pos; i <= pos + n - 1; i++) { … } – for (int *p = pos; p <= pos + n - 1; p++) { … } [Show end.cpp]

Arrays and String Literals What does this print? – #include int main() { char *p; char array[20]; printf("%zd, %zd, %zd\n", sizeof p, sizeof array, sizeof "hello"); }

String Literals What’s wrong with this? – void foo(char *);... foo(“hello”);

What’s a Double-Ended Queue?