Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS 202 12-10-1429.

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Programming and Data Structure
Pointers. andy = 25; fred = andy; ted = &andy; andy = 25; ted = &andy; beth = *ted;
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.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 5 (Loop Statements)
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Computer Science 210 Computer Organization Pointers.
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CPCS 202 Chapter 2 – Input/Output
Microsoft Visual C++.NET Chapter 61 Memory Management.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
LAB#1 : Arrays & Functions. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays Arrays.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 1 (Software Development Method) © CPCS
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Pointers. The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte).
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
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.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
Introduction to Programming Using C
Functions and Pointers
Pointers and Pass By Reference
Hassan Khosravi / Geoffrey Tien
Chapter 14: Dynamic Data Structures
Lecture 6 C++ Programming
Pointers.
Functions and Pointers
Computer Science 210 Computer Organization
Alternate Version of STARTING OUT WITH C++ 4th Edition
Functions, Part 2 of 3 Topics Functions That Return a Value
Chapter 10: Void Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS

CHAPTER 6 - Pointers # Chapter 61. Pointer 1a. Reference Operator 1b. Dereference Operator 1c. Declaring Variables 1. Pointer Illustration 2. Pointers and Functions 3. Ordering Three Numbers  column shows the topics index.  column shows the programs index.

Pointer A. Introduction  Pointers create dynamic data structures  data structures built up from blocks of memory allocated from the heap at run-time  Pointers handle variable parameters passed to functions  Pointers provide an alternative way to access information stored in arrays (Note: you will learn about arrays in the next chapter) B. Subtopics  Reference Operator (&)  Dereference Operator (*)  Declaring Pointers 1

Pointer - Reference Operator (&) A. Introduction  The memory can be imagined as a succession of memory cells  As soon as we declare a variable, the amount of memory needed is assigned for it at a specific location in memory (its memory address)  The address that locates a variable within memory is what we call a reference to that variable  reference to a variable can be obtained by preceding (&), known as reference operator  The variable that stores the reference to another variable is what we call a pointer 1a

Pointer - Reference Operator (&) B. Example 1a andy = 25; fred = andy; ted = &andy; 1.we have assigned the value 25 to andy (a variable whose address in memory is 1776). 2.copied the content of andy to fred. 3.copies the reference of andy to ted.

Pointer - Dereference Operator (*) A. Introduction  Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator  can be literally translated to "value pointed by". B. Example 1b andy = 25; ted = &andy; beth = *ted; *(&andy) == andy

Pointer - Declaring Variables A. Introduction  Due to the ability of a pointer to directly refer to the value that it points to, it becomes necessary to specify in its declaration which data type a pointer is going point to B. Syntax C. Example  int *number;  char *character;  float *greatnumber; 1c

Pointer C. Conclusion  Notice the difference between the reference and dereference operators:  & is the reference operator and can be read as "address of“  * is the dereference operator and can be read as "value pointed by" 1c andy = 25; ted = &andy; beth = *ted; *(&andy) == andy 1

Implementation Problem Analysis Design Outline Testing Maintenance Pointer Illustration P1 #include int main (void) { int firstvalue, secondvalue; int *mypointer; mypointer = &firstvalue; *mypointer = 10; mypointer = &secondvalue; *mypointer = 20; printf("firstvalue is %d\n", firstvalue); printf("secondvalue is %d\n", secondvalue); return 0; }

Implementation Problem Analysis Design Outline Testing Maintenance Pointers and Functions P2 #include void calculation( int num, /* input */ int *cal1, /* output */ int *cal2) /* output */ { *cal1 = num + num; *cal2 = num * num; } int main(void) { int value; /* input: number entered by user */ int sum; /* output: num + num */ int multi; /* output: num * num */ printf("Enter a value to analyze> "); scanf("%d", &value); calculation(value, &sum, &multi); printf("Sum = %d, Multiply = %d\n", sum, multi); return(0); }

Implementation Problem Analysis Design Outline Testing Maintenance Ordering Three Numbers P3

Implementation Problem Analysis Design Outline Testing Maintenance Ordering Three Numbers P3