Introduction to Programming Using C

Slides:



Advertisements
Similar presentations
Pointer Lesson 2 CS1313 Spring Pointer Lesson 2 Outline 1.Pointer Lesson 2 Outline 2.Pass by Reference Bad Example 3.Pass by Reference Good Example.
Advertisements

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.
Functions Prototypes, parameter passing, return values, activation frams.
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
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.
Introduction to Programming Lecture 39. Copy Constructor.
Pointer to Structure. Structure variable can be access using pointers int a=10,*p; Here p  is an integer type pointer variable, p can hold the address.
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.
Week 8 Arrays Part 2 String & Pointer
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 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.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Computer Science 210 Computer Organization Pointers.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
1 Workin’ with Pointas An exercise in destroying your computer.
Topics discussed in this section:
University of Malta CSA2090: Lecture 4 © Chris Staff 1 of 20 CSA2090: Systems Programming Introduction to C Dr. Christopher Staff.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
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.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
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.
Parameter Passing: Arrays 1.Create new variables (boxes) for each of the formal parameters allocated on a fresh stack created for this function call. int.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers What is the data type of pointer variables?
Chapter 8 Arrays, Strings and Pointers
CSE 220 – C Programming Pointers.
Pointers Introduction
© 2016 Pearson Education, Ltd. All rights reserved.
Pointers and Pass By Reference
Pointers and Pointer-Based Strings
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Pointer.
Pointers Psst… over there.
Basic notes on pointers in C
Pointers and References
Pointers Psst… over there.
Pointer Basics Psst… over there.
Buy book Online -
Topics discussed in this section:
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Lecture 18 Arrays and Pointer Arithmetic
Programming in C Pointer Basics.
Functions Pass By Value Pass by Reference
Parameter Passing in Java
Pointers.
Programming in C Pointer Basics.
Simulating Reference Parameters in C
Initializing variables
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers and Pointer-Based Strings
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
C Programming Pointers
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointer Basics Psst… over there.
Pointers and References
Introduction to Pointers
Introduction to Pointers
Presentation transcript:

Introduction to Programming Using C Addresses and Pointers

Contents Returning two results Addresses Bidirectional parameters The void type

Returning Two Results So far, all of our functions have returned a single result Now, suppose we want to pass a number of minutes and have a function return the number of hours and minutes In this case, we need to return two results!

Returning Two Results We can write the function like this int toHM(int totalMins, int residualMins) { int hours = totalMins / 60; residualMins = totalMins % 60; return hours; } Unfortunately, this will not work since residualMins is passed down but not passed back

Returning Two Results The problem is that we pass residualMins by making a copy of its value and passing it Any changes to the copy are not returned to the calling function What we need to do is Pass the address of residualMins Have the function dereference the address to retrieve the value Have the function store the result at the address of residualMins

Addresses So, how do we work with addresses? We can declare a variable to be a pointer to an integer, rather than an integer int *p; This says that p is a variable which points to or stores the address of an integer p is not an integer – it is just the address of an integer This is like saying that the address on an envelope is not your house, just the address of your house

Addresses OK, so now we can store an address But, how do we get an address? & is the address operator that will return the address of a variable * is the dereference operator to get the value stored at an address int *p; /* pointer to an int */ int i = 9; /* a regular int */ p = &i; /* set p to point to i */ *p = 99; /* set value pointed to by p to 99 */ printf(“%d\n”, *p); /* print value pointed to by p, 99 */ * See addressdemo

Bidirectional Parameters We can use addresses and pointers to pass data to a function and return data from a function Declare the parameter as a pointer Use the & operator to generate the address to pass to the function Within the function, use the * operator to access the value that is pointed to by the pointer

Bidirectional Parameters We can now rewrite our function like this int toHM(int totalMins, int *residualMins) { int hours = totalMins / 60; *residualMins = totalMins % 60; return hours; }

Bidirectional Parameters To call this function, we have to pass the address of the variable whose value we want returned main() { int hours, mins, totalMins; totalMins = 200; hours = toHM(totalMins, &mins); printf(“%d hours and %d minutes”, hours, mins); } Note how & is used to create the address of the parameter whose value is to be returned * See paramdemo.c

The void Type Our function returns The hours as the return value The minutes as a bidirectional parameter While this works, it would make more sense to return both values via parameters So what would the function return? Nothing In C this is called void Let’s rewrite our function to return void

Function Using void main() { int hours, mins, totalMins; toHM(totalMins, &hours, &mins); printf(“%d hours and %d minutes”, hours, mins); } void toHM(int totalMins, int *hours, int *residualMins) *hours = totalMins / 60; *residualMins = totalMins % 60; return;

Function Using void Points to note Both hours and minutes are passed as pointers The function toHM no longer returns a value and is not assigned to a variable The dereference operator is used to access all parameters which are passed as pointers The return statement no longer returns a value