Lectures on Numerical Methods1 Address of a variable zEach variable that is declared is stored in memory. Since memory is indexed each variable has an.

Slides:



Advertisements
Similar presentations
Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)
Advertisements

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.
Programming and Data Structure
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.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointers Ethan Cerami Fundamentals of Computer New York University.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
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.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Pointers CSE 2451 Rong Shi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Pointers *, &, array similarities, functions, sizeof.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
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.
Lecture 17: The Last Puzzle Piece with Functions.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
UNIT 8 Pointers.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 Pointers Chapter 07 CMPE-102 Introduction to Programming.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CS1010 Programming Methodology
User-Written Functions
CSE 220 – C Programming Pointers.
Pointers in C.
Pointers and Pointer-Based Strings
Student Book An Introduction
INC 161 , CPE 100 Computer Programming
Programmazione I a.a. 2017/2018.
CSI-121 Structured Programming Language Lecture 16 Pointers
INC 161 , CPE 100 Computer Programming
Object Oriented Programming COP3330 / CGS5409
Instructor: Ioannis A. Vetsikas
Lecture 18 Arrays and Pointer Arithmetic
Pointers Lecture 2 Tue, Jan 24, 2006.
Simulating Reference Parameters in C
Pointers and Pointer-Based Strings
C Programming Pointers
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
Presentation transcript:

Lectures on Numerical Methods1 Address of a variable zEach variable that is declared is stored in memory. Since memory is indexed each variable has an address. zC allows programmers to access the address of the variables. zUnary operator & (read as ‘address of’) gives the address of the operand variable. zSee example. zSince constants, expressions do not have permanent storage, they donot have address.  &1.0, &(d+6) are illegal. zSee printf format. #include #define line "\t \n" main() { int i = 0; char c = 'a'; short s = 1; long l = 2; float f = 3.0; double d = 4.0; printf("Variable Table\n"); printf(line); printf("\tVar\t\tAddress\t\tValue\n"); printf(line); printf("\t%s\t\t%p\t%d\n","i", &i, i); printf("\t%s\t\t%p\t%c\n","c", &c, c); printf("\t%s\t\t%p\t%d\n","s", &s, s); printf("\t%s\t\t%p\t%d\n","l", &l, l); printf("\t%s\t\t%p\t%f\n","f", &f, f); printf("\t%s\t\t%p\t%f\n","d", &d, d); printf(line); } #include #define line "\t \n" main() { int i = 0; char c = 'a'; short s = 1; long l = 2; float f = 3.0; double d = 4.0; printf("Variable Table\n"); printf(line); printf("\tVar\t\tAddress\t\tValue\n"); printf(line); printf("\t%s\t\t%p\t%d\n","i", &i, i); printf("\t%s\t\t%p\t%c\n","c", &c, c); printf("\t%s\t\t%p\t%d\n","s", &s, s); printf("\t%s\t\t%p\t%d\n","l", &l, l); printf("\t%s\t\t%p\t%f\n","f", &f, f); printf("\t%s\t\t%p\t%f\n","d", &d, d); printf(line); }

Lectures on Numerical Methods2 Address of a variable This program was compiled by gcc on linux machine. The output of this program: Variable Table Var Address Value i 0xbffffc94 0 c 0xbffffc93 a s 0xbffffc90 1 l 0xbffffc8c 2 f 0xbffffc d 0xbffffc #include #define line "\t \n" main() { int i = 0; char c = 'a'; short s = 1; long l = 2; float f = 3.0; double d = 4.0; printf("Variable Table\n"); printf(line); printf("\tVar\t\tAddress\t\tValue\n"); printf(line); printf("\t%s\t\t%p\t%d\n","i", &i, i); printf("\t%s\t\t%p\t%c\n","c", &c, c); printf("\t%s\t\t%p\t%d\n","s", &s, s); printf("\t%s\t\t%p\t%d\n","l", &l, l); printf("\t%s\t\t%p\t%f\n","f", &f, f); printf("\t%s\t\t%p\t%f\n","d", &d, d); printf(line); } #include #define line "\t \n" main() { int i = 0; char c = 'a'; short s = 1; long l = 2; float f = 3.0; double d = 4.0; printf("Variable Table\n"); printf(line); printf("\tVar\t\tAddress\t\tValue\n"); printf(line); printf("\t%s\t\t%p\t%d\n","i", &i, i); printf("\t%s\t\t%p\t%c\n","c", &c, c); printf("\t%s\t\t%p\t%d\n","s", &s, s); printf("\t%s\t\t%p\t%d\n","l", &l, l); printf("\t%s\t\t%p\t%f\n","f", &f, f); printf("\t%s\t\t%p\t%f\n","d", &d, d); printf(line); }

Lectures on Numerical Methods3 Pointer to a variable zIf variables have addresses, can these be stored in the variables and manipulated with? zThese addresses have a new data- types (derived data-types) called pointers. zPointers are declared as data-type *var-name; zIn this example one pointer variable has been declared as p and it can store addresses of any integer variables. #include #define line "\t \n" main() { int i = 0; int j = 1; int k = 2; float f = 3.0; int *p = 0; p = &k; printf("p = %p\t\t*p = %d\n", p, *p); j = *p + 5; printf(“j = %d\t\t*p = %d\n", j, *p); *p = *p + i; printf("p = %p\t\t*p = %d\n", p, *p); p = &f; printf("p = %p\t\t*p = %d\n", p, *p); } #include #define line "\t \n" main() { int i = 0; int j = 1; int k = 2; float f = 3.0; int *p = 0; p = &k; printf("p = %p\t\t*p = %d\n", p, *p); j = *p + 5; printf(“j = %d\t\t*p = %d\n", j, *p); *p = *p + i; printf("p = %p\t\t*p = %d\n", p, *p); p = &f; printf("p = %p\t\t*p = %d\n", p, *p); }

Lectures on Numerical Methods4 Pointer to a variable zThe simplest operation is to get an address of a variable and store it in a pointer variable. zExample p = &k; p = &f; zPointer p is declared to store an address of an int variable. zAssigning an address of a float variable to p, would cause a compiler warning or error. (Continuing in spite of warning, may result in disaster). #include #define line "\t \n" main() { int i = 0; int j = 1; int k = 2; float f = 3.0; int *p = 0; p = &k; printf("p = %p\t\t*p = %d\n", p, *p); j = *p + 5; printf(“j = %d\t\t*p = %d\n", j, *p); *p = *p + i; printf("p = %p\t\t*p = %d\n", p, *p); p = &f; printf("p = %p\t\t*p = %d\n", p, *p); } #include #define line "\t \n" main() { int i = 0; int j = 1; int k = 2; float f = 3.0; int *p = 0; p = &k; printf("p = %p\t\t*p = %d\n", p, *p); j = *p + 5; printf(“j = %d\t\t*p = %d\n", j, *p); *p = *p + i; printf("p = %p\t\t*p = %d\n", p, *p); p = &f; printf("p = %p\t\t*p = %d\n", p, *p); }

Lectures on Numerical Methods5 Pointer to a variable zThe value of pointer p is an address of some int variable. zThe value of the int variable pointed to by p can be accessed by using a dereferencing operator *. zPrintf statement here prints the value of p and that of *p. p = 0xbffffc8c *p = 2 z*p is just like k here. All expressions where k can appear, *p also can. j = 7 *p = 2 #include #define line "\t \n" main() { int i = 4; int j = 1; int k = 2; float f = 3.0; int *p = 0; p = &k; printf("p = %p\t\t*p = %d\n", p, *p); j = *p + 5; printf(“j = %d\t\t*p = %d\n", j, *p); *p = *p + i; printf("p = %p\t\t*p = %d\n", p, *p); p = &f; printf("p = %p\t\t*p = %d\n", p, *p); } #include #define line "\t \n" main() { int i = 4; int j = 1; int k = 2; float f = 3.0; int *p = 0; p = &k; printf("p = %p\t\t*p = %d\n", p, *p); j = *p + 5; printf(“j = %d\t\t*p = %d\n", j, *p); *p = *p + i; printf("p = %p\t\t*p = %d\n", p, *p); p = &f; printf("p = %p\t\t*p = %d\n", p, *p); }

Lectures on Numerical Methods6 Pointer to a variable zThe dereferencing operator *p is not only used to get the value of the variable pointed to by p, but also can be used to change the value of that variable. zIt is legal to use *p as left side of an assignment. P = 0xbffffc8c *p = 6 #include #define line "\t \n" main() { int i = 0; int j = 1; int k = 2; float f = 3.0; int *p = 0; p = &k; printf("p = %p\t\t*p = %d\n", p, *p); j = *p + 5; printf(“j = %d\t\t*p = %d\n", j, *p); *p = *p + i; printf("p = %p\t\t*p = %d\n", p, *p); p = &f; printf("p = %p\t\t*p = %d\n", p, *p); } #include #define line "\t \n" main() { int i = 0; int j = 1; int k = 2; float f = 3.0; int *p = 0; p = &k; printf("p = %p\t\t*p = %d\n", p, *p); j = *p + 5; printf(“j = %d\t\t*p = %d\n", j, *p); *p = *p + i; printf("p = %p\t\t*p = %d\n", p, *p); p = &f; printf("p = %p\t\t*p = %d\n", p, *p); }

Lectures on Numerical Methods7 Pointer Arithmetic zA simple arithmetic is allowed for the pointers. zA pointer points a variable of a given type. When we add 1 to a pointer variable, it points to the next variable in the memory(though it may not be of the same type). zHere p = p + 1 would cause the value of p to be added 4 which is size of int. zLook at output. #include #define line "\t \n" main() { int i = 0; int j = 1; int k = 2; int *p = 0; p = &k; printf("p = %p\t\t*p = %d\n", p, *p); p = p + 1; printf("p = %p\t\t*p = %d\n", p, *p); p++ ; printf("p = %p\t\t*p = %d\n", p, *p); p = &f; printf("p = %p\t\t*p = %d\n", p, *p); } #include #define line "\t \n" main() { int i = 0; int j = 1; int k = 2; int *p = 0; p = &k; printf("p = %p\t\t*p = %d\n", p, *p); p = p + 1; printf("p = %p\t\t*p = %d\n", p, *p); p++ ; printf("p = %p\t\t*p = %d\n", p, *p); p = &f; printf("p = %p\t\t*p = %d\n", p, *p); }

Lectures on Numerical Methods8 Pointer Arithmetic zVariable Table Var Address Value i 0xbffffc94 0 j 0xbffffc90 1 k 0xbffffc8c 2 f 0xbffffc p 0xbffffc84 (nil) zThe output of this program is given here. p = 0xbffffc8c *p = 2 p = 0xbffffc90 *p = 1 p = 0xbffffc94 *p = 0 p = 0xbffffc88 *p =

Lectures on Numerical Methods9 Pointers and Functions Arguments çWe have seen that since a and b are passed by value to swap1 function, the values of a and b in main are not swapped. çBut in swap2, the address of a and address of b is passed. Hence pa and pb contains the addresses of a and b. Then the changes are made directly to the locations of a and b. çThe result would be void swap1(int a, int b) { int temp = a; a = b; b = temp; } void swap2(int *pa, int *pb) { int temp = *pa; *pa = *pb; *pb = temp; } main() { int a = 3, b = 5; swap1(a, b); printf(“%d\t%d”, a, b); swap2(&a, &b); printf(“%d\t%d”, a, b); } void swap1(int a, int b) { int temp = a; a = b; b = temp; } void swap2(int *pa, int *pb) { int temp = *pa; *pa = *pb; *pb = temp; } main() { int a = 3, b = 5; swap1(a, b); printf(“%d\t%d”, a, b); swap2(&a, &b); printf(“%d\t%d”, a, b); }

Lectures on Numerical Methods10 Pointers and Functions Arguments çIt is indeed inconvenient that a function can return only one value. çConsider a function for the bisection method. The function is expected to return a root of a function. double getRoot(double l, double r, double tol, int maxIter); çWhat would happen if the root is not bracketed in [l,r] interval? The function is still likely to return some value which is not the required root. Ideally, there should be one more variable indicating the error. double getRoot(double l, double r, double tol, int maxIter, int *error);

Lectures on Numerical Methods11 Pointers and Arrays çAn array name is a constant pointer. So if a is an array of 10 integers and pa is a pointer to int variable, then Pa = &a[0]; Pa = a; çAre legal and same. So are B = a[5]; B = *(a+5); çHowever since a is constant pointer following is invalid A = pa;