CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
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.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Computer Science 210 Computer Organization Pointers.
Pointers CSE 2451 Rong Shi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
(continue) © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lecture 17: The Last Puzzle Piece with Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Engineering Computing I Chapter 5 Pointers and Arrays.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Chapter 8 Arrays, Strings and Pointers
Computer Skills2 for Scientific Colleges
Week 9 - Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 - Pointers Outline 7.1 Introduction
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
8 Pointers.
Lecture 18: The Elegant, Abstract World of Computing
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
5.1 Introduction Pointers Powerful, but difficult to master
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
CISC181 Introduction to Computer Science Dr
Programming fundamentals 2 Chapter 3:Pointer
C Pointers Another ref:
Presentation transcript:

CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004

Pointers Introduction Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain address of a variable that has a specific value (indirect reference) Indirection – referencing a pointer value count 7 7 count_ptr

Pointer Declarations Pointer declaration: int *my_ptr; Declares a pointer to an int (pointer of type int *) Multiple pointers require using a * before each variable declaration int *my_ptr1, *my_ptr2; Can declare pointers to any data type. Multiple pointers can point to same thing.

The & and * operators The & (address of) operator returns address of operand, the * (de-referencing) operator gives back the variable pointed to. int y = 5, *y_ptr; y_ptr = &y;/* y_ptr set to address of y */ (*y_ptr)++;/* increment y */ Note that * and & are inverses: printf("y is %d\n", **&&y); /* silly example */ You can initialize pointers to 0 (or NULL) or an address: char c *char_ptr = NULL;

Pointers Example The following program has a pointer variable, ptr_to_number, declared: #include void main(){ int number = 3; int *ptr_to_number; ptr_to_number = &number; printf(“Value of number is %d\n”, *ptr_to_number); }

Pointers Example (2) Two variables are declared, number and ptr_to_number. number is set equal to 3 and ptr_to_number is not initialised. The assignment statement sets the variable ptr_to_number to “point” to numbe r. This is achieved by means of the address-of operator, &. The output to the screen will be. Value of number is 3 You can also change the value of a variable by accessing it via a pointer. *ptr_to_number = 10; printf(“Value of number is %d\n”, *ptr_to_number); The output is now: Value of number is 10

Pointer Types A pointer to one type cannot be assigned to the address of another type, e.g.: int i, *ptr_to_int; char c; &ptr_to_int = c;/* error */ In C it is conventional to assign pointers to NULL, which has the value 0. char *c_ptr = NULL; The dereferencing operator (*) has the same high precedence as the other unary operators such as ++, and right-to-left associativity.

Arrays and Pointers Arrays and pointers are closely related, for example pointers can perform array subscripting. Declares an array b[5] and a pointer b_ptr and sets them equal to one another: char b[5], *b_ptr; b_ptr = b; The array name (b) is actually the address of first element of the array, &b[0].

Pointer Arithmetic Element b[3] can be accessed with *(b_ptr + 3) where n is the offset. This is called pointer/offset notation. Note that b_ptr[3] is also the same as b[3]. Elements can also be accessed by doing pointer arithmetic on the array itself *(b + 3) There are differences between arrays and pointers, of course. You cannot assign one array to another, e.g. int a[10], b[10]; a = b;/* illegal */

Pointer Comparison You can also compare pointers for equality and inequality: two pointers are equal if they point to the same variable or to the same cell in an array, and are unequal if they don't. One common use of pointer comparisons is when copying arrays using pointers. int array1[10], array2[10]; int *ip1, *ip2 = array2; int *end_ptr = &array1[10]; for(ip1 = array1; ip1 != end_ptr; ) *ip2++ = *ip1++;

Pass by reference (1) Parameter passing so far has been pass-by-value. We will illustrate this with an example function, add_VAT, which accepts the price of an item as an argument and computes the value-added tax (VAT) for the item, returning the price plus VAT. #define VATRATE 0.2 float price_item; add_VAT(price_item); printf(“Price plus VAT is %f\n”, price_item); void add_VAT(float local_price_item){ local_price_item*=(1 + VATRATE); }

Pass by reference (2) The code above will not work properly because local _price_item is a copy of price_item and this value is not copied back to price_item. We need to pass- by-reference and the correct way to achieve this would be to use pointers. add_VAT(&price_item); printf(“Price plus VAT is %d\n”, price_item); void add_VAT(float *ptr_price_item){ *ptr_price_item*=(1 + VATRATE); } Note that the address-of operator ( & ) is required before the argument to add_VAT. The parameter is declared as a pointer to float.