Agenda  Perform Quiz#3 (15 Minutes)  Introduction to Pointers –What are pointers? / Why use pointers? –Pointer Terminology Memory Address format specifier.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

An introduction to pointers in c
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.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Programming and Data Structure
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Pointers Ethan Cerami Fundamentals of Computer New York University.
Topic 8 – Introduction to Pointers and Function Output Parameters.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Agenda  Perform Quiz#2 (20 Minutes)  Functions / Continued … –Functions - Definition –Types of Functions: Functions that do not accept or return a value.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
CPS120: Introduction to Computer Science Lecture 15A Structures.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
CPS120: Introduction to Computer Science Data Structures.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
UNIT 8 Pointers.
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.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CS1010 Programming Methodology
CS1010 Programming Methodology
User-Written Functions
Introduction to Programming
Introduction to Programming Using C
COMP 2710 Software Construction Pointers
Pointers Psst… over there.
Basic notes on pointers in C
14th September IIT Kanpur
Pointers Psst… over there.
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
5.1 Introduction Pointers Powerful, but difficult to master
7. Pointers, Dynamic Memory
Pointers Pointers point to memory locations
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
Pointers and References
C++ Programming Lecture 17 Pointers – Part I
Data Structures and Algorithms Introduction to Pointers
Pointers and dynamic objects
Dynamic Memory Allocation
Chapter 10: Void Functions
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Introduction to Pointers
Introduction to Pointers
Presentation transcript:

Agenda  Perform Quiz#3 (15 Minutes)  Introduction to Pointers –What are pointers? / Why use pointers? –Pointer Terminology Memory Address format specifier Pointer Variable De-referencing a Pointer Indirection –Examples

Consider In Class Exercise #2 From Previous Class: #include #define GST 7.0; #define PST 8.0; double Calc_Taxes (double); int main (void){ int quantity = 10;/* Declare Variables */ double price = 2.25, amount, taxes, total_amount; amount = quantity * price; /* Calculate amount, taxes & total */ taxes = Calc_Taxes (amount); total_amount = amount + taxes; printf ("\nSales:\t$%.2lf\tTaxes:\t$%.2lf\tTotal:\t$%.2lf\n\n", amount, taxes, total_amount); return 0; } double Calc_Taxes (double amt){ double pst, gst, total_taxes; pst = amt * (PST / 100); /*Calculate PST and GST & Total Taxes */ gst = amt * (GST / 100); total_taxes = pst + gst; return total_taxes; } Function Calc_Taxes was used to calculate and return total taxes to the main program.

What if We Want to Return Both PST and GST Values? Let’s take a closer look at the Calc_Taxes function: double Calc_Taxes (double amt, double pst, double gst){ double total_taxes; pst = amt * (pst / 100); /*Calculate PST and GST & Total Taxes */ gst = amt * (gst / 100); total_taxes = pst + gst; return total_taxes; } The variables pst and gst are local to the Calc_Taxes function only, the only value in this function that is returned is total_taxes

How to Solve the Problem?  There are two methods to have more than one variable passed-back when running a function:  Method #1 – Use Global Variables  This method is not recommended since many people may work on different functions and not be aware of global variable. They may change global variable without realizing effects from change in entire program.  Method #2 – Use Pointers  Use pointers as a “trick” to point to the memory address of an already existing variable.  This method is recommended since programmers can create their own variables that will not affect global variables, thus affecting the entire program.

What are Pointers? Pointer: A pointer is a value or variable that contains the memory address that stores a value of another variable. This provides a “trick” to change the values of variables within other functions

Steps to Use Pointers Step 1: Declare a Pointer –The pointer is declared (usually in the function header). –An asterisk “*” (the indirection operator) in front of a variable name is used to access a specific memory location of a variable. –To avoid confusion with a pointer, it is recommended to place a “p” or “ptr” to indicate that a variable is a pointer. eg. int *p_gst, *p_gst;

Steps to Use Pointers Step 2: Assign Memory Address to Pointer –In order to use a pointer, we must assign the memory address of another variable to the pointer variable. eg. *p_gst = &gst; *p_pst = &pst; Calc_Taxes (amount, *p_pst, *g_gst); –When using functions, it may be easier to pass the memory address to the function instead: eg. Calc_Taxes (amount, &gst, &pst);

Steps to Use Pointers Step 3: Use a Pointer to change Original Variable –When calling a function, the value of the memory address is passed to a pointer variable. –Within the function, the pointer variable is used to change the value of the original variable – eg. int *p_gst, *p_gst;

Solution to Program Using Pointers #include #define GST 7.0 #define PST 8.0 void Calc_Taxes (double, double *, double *); int main (void){ int quantity = 10; double price = 2.25, amount, taxes, total_amount; double pst, gst; amount = quantity * price; Calc_Taxes (amount, &gst, &pst); taxes = pst + gst; total_amount = amount + taxes; printf ("\nSales:\t\t$%.2lf\n\n", amount); printf ("Taxes:\n"); printf (" GST:\t\t$%.2lf\n PST:\t\t$%.2lf\n", gst, pst); printf ("Total Taxes:\t$%.2lf\n\n", taxes); printf ("Invoice Total:\t$%.2lf\n\n", total_amount); return 0; } void Calc_Taxes (double amt, double *p_gst, double *p_pst){ *p_pst = amt * (PST / 100); *p_gst = amt * (GST / 100); } Notice that with Pointers, we didn’t have to to return any value from the function!!