CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Similar documents
CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

CPSC 427: Object-Oriented Programming

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

[0569] p 0318 garbage

CSC 211 Intermediate Programming. Arrays & Pointers

a data type is Types

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Arrays and Pointers (part 1)

Arrays, Pointers and Memory Management

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

CS 261 Data Structures. Introduction to C Programming

EL2310 Scientific Programming

Fundamentals of Programming Session 20

CSCI 2212: Intermediate Programming / C Working with Pointers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Goals of this Lecture

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Basic Pointers. CSCI 112: Programming in C

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS201- Introduction to Programming Current Quizzes

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

Type Checking. Prof. James L. Frankel Harvard University

CPSC 427a: Object-Oriented Programming

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

CSCI 171 Chapter Outlines

CSCI 2212: Intermediate Programming / C Storage Class and Dynamic Allocation

CPSC 427: Object-Oriented Programming

Fundamentals of Programming Session 19

Lecture 12 Integers. Computer and Network Security 19th of December Computer Science and Engineering Department

Understanding Pointers

Arrays and Pointers (part 1)

Pointers. Memory. void foo() { }//return

First of all, it is a variable, just like other variables you studied

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Strings Investigating Memory Allocation Pointers Fixed-Point Arithmetic. Memory Matters. Embedded Systems Interfacing.

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

by Pearson Education, Inc. All Rights Reserved.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Assist. Prof. Dr. Caner ÖZCAN

CS 61c: Great Ideas in Computer Architecture

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

Midterm Sample Questions. a) What evaluates to FALSE in C? What evaluates to TRUE?

Java Primer 1: Types, Classes and Operators

CS31 Discussion 1E Spring 17 : week 08

CMSC 202. Pointers Dynamic Memory Allocation

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Programming in C++ 6. Floating point data types

BASIC ELEMENTS OF A COMPUTER PROGRAM

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Data Structures and Algorithms

Homework #3 CS2255 Fall 2012

Data Representation and Storage

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

Introduction to Computer Science Midterm 3 Fall, Points

Module 6: Array in C

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Pointers. 10/5/07 Pointers 1

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

CS2141 Software Development using C/C++ C++ Basics

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Array. Prepared By - Rifat Shahriyar

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Data Representation and Storage. Some definitions (in C)

CS 430 Computer Architecture. C/Assembler Arithmetic and Memory Access William J. Taffe. David Patterson

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Object-Oriented Principles and Practice / C++

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

C Syntax Out: 15 September, 1995

ECE 30 Introduction to Computer Engineering

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CS 161 Exam II Winter 2018 FORM 1

Static Code Analysis - CERT C Secure Code Checking

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Chapter 1 Getting Started

Unit 3. Constants and Expressions

The type of all data used in a C (or C++) program must be specified

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Introduction to Scientific Computing and Problem Solving

Lecture 05 POINTERS 1

Writing Functions in C

CSCI 2212: Intermediate Programming / C Recursion

Transcription:

... 1/16 CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11 Alice E. Fischer February 3, 2016

... 2/16 Outline Basic Types and Diagrams

... 3/16 Basic Types and Diagrams Types in C C has many built-in types. These include: int and unsigned int short and unsigned short long and unsigned long char, signed char, and unsigned char bool (since C-99) float, double, and long double Chapter 7 lists the range of values that can be stored in each type and gives the limits of precision for the floating types.

... 4/16 Basic Types and Diagrams Pictures of the Basic Types. In the diagrams, the size of the box is proportional to the number of bytes needed to store a value. char or bool short int, float, or pointer double an array of char

... 5/16 Diagrams of pointers Pointer operations

... 6/16. int k = 3; int* p; int* q = NULL; int* pi = &k; A pointer, uninitialized p? A NULL pointer A pointer to an integer After executing p = pi; q pi p 0 (memory location 0) 3 k

... 7/16 Pointer Details A pointer is an address. We say it has star-level 1. An int has star-level 0. The address of an int has level 1. A pointer variable is a variable that stores an address. are used to process arrays. An array has star-level 1 are used to return answers from functions. int k = 5; int ary[4] = {}; // An array: 4 slots initialized to 0 int* p1 = &k; // Star-level must match. int* p2 = arr; int* p3 = &arr[0]; // Same as line above. int* q = nullptr; // In C, use NULL.

... 8/16 and Arrays. int k = 5; int ary[4] = {}; int* p1 = &k; int* p2= arr; int* q = nullptr k 5 arr [0] [1] [2] [[3] 0 0 0 0 p1 p2 q

... 9/16 Pointer Semantics The following things make sense ONLY if pointers p and q are pointing into the same array. Pointer arithmetic works in units of array slots, not bytes. Point to the first byte that is NOT in the array. Add int N to p to move p N slots toward end of the array. Subtract q-p to find the # of array slots between p and q Compare p==q to find if they point to the same array slot Compare p > q to learn which one is at a larger subscript

... 10/16 Pointer Syntax The name of an array is translated as a pointer to its slot 0. The slot 0 of an array is guaranteed to be at a lower address than slot 1. In a declaration, the * is part of the type, not part of the variable name. Avoid declaring more than one pointer in a declaration, since the * must be repeated for each, and that is too easy to forget. In the code, *p means the contents of the variable p points at. In the code, p means the address that p points at.

... 11/16 Using *. int k = 7; int arr[3]; int* p = arr; int* q = &k; q p 7096 8000 7096 k 7 8000 8004 8008??? arr *p = *q; p = q; q p 7096 7096 7096 7 8000 8004 8008 7?? If you write p =, you will change the contents of p. If you write *p = you will change the value of whatever p points at. The star level on both sides of the = must be the same.

... 12/16 Usage: array processing. are often used to process arrays. Here is how: Establish a single name that represents the beginning of the array, either the name you gave it when you declared it, or the name of the pointer in which you store the result of a new dynamic allocation. This is the name to use when you free the dynamic memory. Create an end pointer to point at the first address that is not in the array. Use this to end loops. Create a scanning pointer that starts at the head and goes to the tail. p1 = arr; // Point at head of the array. p2 = arr+4; // Point at the first non-array slot. q = p1; // Start a scanning pointer at the head. for (q=p1; q<p2; ++q)... // process the whole array.

... 13/16 Head and Tail, Cursors. p1 = arr; // This is a stationary head pointer. p2 = arr+4; // This is an offboard tail pointer. q = p1; // This is a scanning pointer. for (q=p1; q<p2; ++q)... p1 arr [0] [1] [2] [3] 0 0 0 0? q p2

... 14/16 Pointer arithmetic, L- and R-values. Some error comments and most professional documentation talks about L-values and R-values. int k = 7; int* q = &k; q 7096 7096 7 k The L-value of k is 7096. Its R-value is 7. The R-value of q is 7096. *q is the same as k. int arr[3] = {2, 3, 5} int* p = arr; int* end = arr + 3; p 8000 end 8012 arr 8000 8004 8008 2 3 5 8000 8004 8008 After ++p p 8004 11 3 5

... 15/16 Array Parameters An array argument is ALWAYS passed by address. This saves time and space. In the function call, use the array name, without any & or subscript. In the function, declare the parameter to be a pointer or an array; it makes little difference. Inside the function, you can use subscripts, even if the parameter is a * type. The length of the array is NOT passed to the function as part of the array, so it must be passed separately. If you declare an array parameter with a length, the length will be ignored.

... 16/16 Pointer Parameters A pointer parameter can be used to return an answer from a function. For example, to return a double, you might declare the parameter to be double* db. To call the function, you would provide an array argument or the address of a non-array. Inside the code, near the end, you would write *db = answer; This will deliver the answer back to the caller s variable.