Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays.

Slides:



Advertisements
Similar presentations
Chapter 16 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Advertisements

Lectures 10 & 11.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Kernighan/Ritchie: Kelley/Pohl:
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.
Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
Overview Projects – Project 1’s Project 2’s no later than Dec 14th Homework Problem – 14.X Pointer Variables Pass by Value Pass by Reference (or by Pointer)
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
1 Review of Chapter 6: The Fundamental Data Types.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
Computer Science 210 Computer Organization Pointers.
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
 Using functions  Writing functions  Basics  Prototypes  Parameters  Return types  Functions and memory  Pointer parameters.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
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.
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Windows Programming Lecture 03. Pointers and Arrays.
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.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
CSE 220 – C Programming Pointers.
Pointers.
INC 161 , CPE 100 Computer Programming
Pointer.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 16 Pointers and Arrays
More C Stack Frames / Pointer variables
Chapter 16 Pointers and Arrays
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Pointers and Arrays.
Programming in C Pointer Basics.
Chapter 16 Pointers and Arrays
Programming in C Pointer Basics.
Chapter 16 Pointers and Arrays
C++ Pointers and Strings
ECE Application Programming
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Programming in C Pointer Basics.
CSCE 206 Lab Structured Programming in C
C++ Pointers and Strings
Chapter 16 Pointers and Arrays
Presentation transcript:

Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays

Pointers in C Declaration int *p; /* p is a pointer to an int */ A pointer in C is always a pointer to a particular data type: int*, double*, char*, etc. Operators *p -- returns the value pointed to by p &z -- returns the address of variable z

Declaring Pointer variables int *ptr; ptr is a pointer variable that points to an int variable char *cp; cp is a pointer variable that points to a character variable double *dp; dp is a pointer variable that points to a double variable * is referred to as the indirection operator, or the dereference operator

Address Operator & int object; int *ptr; object = 4; ptr = &object pointer variable ptr points to variable object ptr = null pointer not = 0

Example Define local variables: int object; int *ptr; Now, let’s assign values to them: object = 4; ptr = &object *ptr = *ptr + 1; The last statement is equivalent to: object = object + 1

Example: Parameter Passing by Value #include void Swap(int firstVal, int secondVal); int main() { int valueA = 3; int valueB = 4; Swap(valueA, valueB); return 0; } void Swap(int firstVal, int secondVal) { int tempVal; /* Holds firstVal when swapping */ tempVal = firstVal; firstVal = secondVal; secondVal = tempVal; return; } Snapshot before return from subroutine

Example: Parameter Passing by Reference #include void NewSwap(int *firstVal, int *secondVal); int main() { int valueA = 3; int valueB = 4; NewSwap(&valueA, &valueB); return 0; } void NewSwap(int *firstVal, int *secondVal) { int tempVal; /* Holds firstVal when swapping */ tempVal = *firstVal; *firstVal = *secondVal; *secondVal = tempVal; return; } Snapshots during the exchange

Scanf( ) function Recall reading from the keyboard in C: scanf(“%d”, &input); Why do we use &input ?

Pointer Example #include int IntDivide(int x, int y, int *quoPtr, int *remPtr); int main() { int dividend; /* The number to be divided */ int divisor; /* The number to divide by */ int quotient; /* Integer result of division */ int remainder; /* Integer remainder of division */ int error; /* Did something go wrong? */ printf("Input dividend: "); scanf("%d", &dividend); printf("Input divisor: "); scanf("%d", &divisor); error = IntDivide(dividend,divisor,&quotient,&remainder); if (!error) /* !error indicates no error */ printf("Answer: %d remainder %d\n", quotient, remainder); else printf("IntDivide failed.\n"); } int IntDivide(int x, int y, int *quoPtr, int *remPtr) { if (y != 0) { *quoPtr = x / y; /* Modify *quoPtr */ *remPtr = x % y; /* Modify *remPtr */ return 0; } else return -1; }