Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.

Slides:



Advertisements
Similar presentations
Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)
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.
Chapter 6 Data Types
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
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Data Types C built-in data types –char, int, float, double, int*, etc. User-defined data types: the programmer can define his/her own data types which.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Ethan Cerami Fundamentals of Computer New York University.
C language issues CSC 172 SPRING 2002 EXTRA LECTURE.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
Computer Science 210 Computer Organization Pointers.
Pointers CSE 2451 Rong Shi.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
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.
1 ร. ศ. ดร. สุเทพ มาดารัศมี Understanding Pointers in C Chapter 10 of Programming with C Book.
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:
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.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Homework Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
What does the following code write to the monitor? #include int main( void ) { int a ; int *pa ; a = 77 ; pa = &a ; printf("a=%d *pa=%d\n", a, *pa ); system("pause");
Functions & Pointers in C Jordan Erenrich
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
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 and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
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.
Pointers Data Structures CSI 312 CSI Dept. Dr. Yousef Qawqzeh.
Pointers What is the data type of pointer variables?
CS1010 Programming Methodology
“Studying C programming excluding pointers is meaningless.” d0m3z
CSE 220 – C Programming Pointers.
Standard Version of Starting Out with C++, 4th Edition
Pointers and Pointer-Based Strings
Command-Line Arguments
Pointer.
INC 161 , CPE 100 Computer Programming
Basic notes on pointers in C
Tejalal Choudhary “C Programming from Scratch” Pointers
Buy book Online -
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Pointers & Functions.
Topics discussed in this section:
Homework Finishing K&R Chapter 5 today Starting K&R Chapter 6 next
Homework Starting K&R Chapter 5 Good tutorial on pointers
Pointers and Pointer-Based Strings
C Programming Pointers
Chapter 9: Pointers and String
Pointers & Functions.
Standard Version of Starting Out with C++, 4th Edition
Pointers and pointer applications
Introduction to Pointers
Presentation transcript:

Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value 2. Pointer variables hold addresses: specific locations in computer's memory. Pointers also have different types: int *p; declare a pointer p of type int. The variable p can hold addresses of ints.

Address Operator & & when applied to a variable produces the address of that variable. int x; int *pt; pt = &x; The first line defines the variable x. It is in a specific location in memory, say at 9640 (byte position). The second line defines a pointer variable of type int. The last line assigns pt to hold the value of the address of x. I.e., pt contains the value We say pt points to x. x pt 9640

Dereference * Given that int x; int *pt; pt = &x; when pt appears in an expression, it gives you the value of pt. That is the address of variable x (say 9640). When *pt appears in an expression, it gives the value of the variable pt is pointing to. That is, the value of x (if pt has the address of x). This is called dereference.

Dereference, example int x, y; int *pt; pt = &x; /* pt gets the address of x */ x = 3; /* x gets 3 */ y = *pt; /* y gets the value that the pointer pt points to. Since pt has the address of x, y get the value of x. That is, y gets 3 */ *pt = 4; /* The variable at the address that pt is pointing to gets the value 4. That is, x reassigns to the value 4 */

Type of Pointers int x; char c; float f; int* ptx; char *ptc; float *ptf; ptx = &x; /* OK */ ptx = &c; /* WRONG */ ptc = &c; /* OK */ ptf = &f; /* OK */ ptf = &x; /* WRONG */ Int pointer can hold address of int variable only. Char pointer can hold address of char variable only.

Pointer Example, Swap two values with pointer char c1 = 'A'; char c2 = 'Z'; char temp, *p; p = &c1; temp = *p; *p = c2; c2 = temp; c1 c2 temp p AZ p 10 c1 c2 temp AZA ZZA ZAA

Pointers/Swap Addresses #include main() { char c = 'O', d= 'H'; char *p1, *p2, *temp; p1 = &c; p2 = &d; printf("%c%c", *p1, *p2); temp = p1; p1 = p2; p2 = temp; printf("%c%c", *p1, *p2); } The program prints: OHHO Swap the values of p1 and p2.

Initializing a Pointer int i; int *p = &i; The declarations allocate storage for two cells, int i and pointer to int p. It also initializes p to have the address of i. Variable i must be declared before p in order to use it in the expression &i.

Passing Ordinary Variable v.s. Passing Pointer #include void func(int i, int *p); main() { int i = 1, j = 2; printf("%d %d\n", i, j); func(i,&j); printf("%d %d\n", i, j); } void func(int i, int *p) { ++i; *p = *p + 1; } prints:

Levels of Indirection main() { int x; /* a int value */ int *p; /* pointer to int */ int **pp; /* pointer to pointer to int */ x = 5; p = &x; pp = &p; printf("%d %d %d\n", x, *p, **pp); }

Levels of Indirection int x; /* int value */ int *p; /* pointer to int */ int **pp; /* pointer to pointer to int */ x = 5; p = &x; pp = &p; 5 Address x p pp

Pointer to a pointer to a pointer … to char #include main() { char c='A'; char *p; char **p2; char ***p3; p = &c; p2 = &p; p3 = &p2; printf("%c%c%c%c\n", c, *p, **p2, ***p3); printf("%d\t%p\n", c, &c); printf("%p\t%p\n", p, &p); printf("%p\t%p\n", p2, &p2); printf("%p\t%p\n", p3, &p3); } AAAA 65 4c4a3 4c4a3 4c49c 4c49c 4c498 4c498 4c494 %p prints address in hexadecimal.

Reading/Home Working Read Chapter 7, page 306 to 319. Work on Problems –Section 7.1, page 315, exercise 1, 3, 5. –Section 7.2, page 320, exercise 1, 3, 5, 7. Check your answers in the back of the textbook. Do not hand in.