Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering

Slides:



Advertisements
Similar presentations
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Advertisements

Kernighan/Ritchie: Kelley/Pohl:
CPT: Pointers/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (pointers)
Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Topic 2 Pointers CSE1303 Part A, Summer Semester,2002 Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A2 – Pointers (Revision)
1 CSE1301 Computer Programming Lecture 16 Pointers.
1 CSE1301 Computer Programming Lecture 16 Pointers 2.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Pointers Ethan Cerami Fundamentals of Computer New York University.
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Pointers Applications
1 CSE1301 Computer Programming Lecture 16 Pointers.
1 Review of Chapter 6: The Fundamental Data Types.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Pointers CSE 2451 Rong Shi.
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.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
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.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Object-Oriented Programming in C++
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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Lecture 17: The Last Puzzle Piece with Functions.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers (Revision). 2 Overview Revision of Pointers Pointers and structs Basic Pointer Arithmetic Pointers and Arrays.
Lecture 5 Pointers 1. Variable, memory location, address, value
Computer Skills2 for Scientific Colleges
CSE 220 – C Programming Pointers.
Pointers.
Standard Version of Starting Out with C++, 4th Edition
© 2016 Pearson Education, Ltd. All rights reserved.
POINTERS.
EPSII 59:006 Spring 2004.
INC 161 , CPE 100 Computer Programming
Pointer.
Lecture 6 C++ Programming
CSI-121 Structured Programming Language Lecture 16 Pointers 2
CSI-121 Structured Programming Language Lecture 16 Pointers
Programming fundamentals 2 Chapter 2:Pointer
Chapter 9: Pointers.
Chapter 9: Pointers.
Topics discussed in this section:
Programming with Pointers
Computer Skills2 for Scientific Colleges
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
C Programming Lecture-8 Pointers and Memory Management
Standard Version of Starting Out with C++, 4th Edition
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering

Topics Introduction to Pointers Pointers and Function Parameters

Introduction Pointer is one of the most powerful features of C Pointers enable programs to simulate –call by reference –to create and manipulate dynamic data structures (that can grow and shrink)

Computer Memory Computer’s memory consists of many thousands of sequential storage locations, and each location is identified by a unique address –From 0 to maximum When you declare a variable in a C program, the compiler set aside a memory location with a unique address to store that variable –When your program uses the variable name, it automatically accesses the proper memory location

char ch = ’A’; ’A’ 0x2000 ch: Memory Address of a Variable The value of the variable ch. The memory address of the variable ch.

The & Operator Gives the memory address of an object Also known as the “address operator.” &ch yields the value 0x2000 char ch = ’A’; ’A’ 0x2000

“conversion specifier” for printing a memory address char ch; printf(“%p”, &ch); Example:

Pointers ch 0x1FFF0x20000x20010x20020x1FFE etc. ‘B’ 0x2000 chPtr 0x3A15 A variable which can store the memory address of another variable.

Pointers A variable directly references a value A pointer indirectly references a value count 7 7 countPtr

Pointers A pointer is a variable Contains a memory address Points to a specific data type Pointer variables are usually named varPtr To declare a pointer, we use the following form typename *ptrname;

cPtr: char* cPtr; Example: We say cPtr is a pointer to char. 0x2004 Can store an address of variables of type char

Pointers and the & Operator Example: A c: 0x2000 char c = ’A’; char *cPtr; cPtr: 0x2004 cPtr = &c; 0x2000 Assigns the address of c to cPtr.

Notes on Pointers int* numPtr; float* xPtr; Example: We can have pointers to any data type. int *numPtr; float * xPtr; char* chPtr; Example: The * can be anywhere between the type and the variable.

Notes on Pointers (cont.) You can assign the address of a variable to a “compatible” pointer using the & operator. intaNumber; int*numPtr; numPtr = & aNumber; Example:

Notes on Pointers (cont.) You can print the address stored in a pointer using the %p conversion specifier. printf(“%p”, numPtr); Example:

Notes on Pointers (cont.) int *numPtr = NULL; NULL numPtr When declaring a pointer, it is a good idea to always initialize it to NULL (a special pointer constant).

The * Operator Allows pointers to access to variables they point to Also known as “dereferencing or indirection operator” Should not be confused with the * in the pointer declaration or the multiplication operator Don't worry about the compiler's becoming confused

A c: 0x2000 B Pointers and the  Operator Example: char c = ’A’; char *cPtr = NULL; cPtr: 0x2004 cPtr = &c; 0x2000 *cPtr = ’B’; Changes the value of the variable which cPtr points to.

Pointers and the  Operator int rate; int *p_rate; rate = 100; p_rate = &rate; printf(“%d”,rate); printf(“%d”,*p_rate); 100

Pointer Cautions int *numPtr; *numPtr = 12; Beware of pointers which are not initialized! ??? numPtr

Easy Steps to Pointers Step 1: Declare the variable to be pointed to. intnum; charch = ‘A’; float x; num: ‘A’ ch: x:

Easy Steps to Pointers Step 2: Declare the pointer variable. int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; intnum; charch = ‘A’; float x; numPtr: NULL chPtr: NULL xPtr: num: ‘A’ ch: x:

Easy Steps to Pointers Step 3: Assign address of variable to pointer. int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; intnum; charch = ‘A’; float x; numPtr = # chPtr = &ch; xPtr = &x; numPtr: addr of num addr of ch chPtr: addr of x xPtr: num: ‘A’ ch: x: A pointer’s type has to correspond to the type of the variable it points to.

Easy Steps to Pointers Step 4: De-reference the pointers. int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; intnum; charch = ‘A’; float x; numPtr = # chPtr = &ch; xPtr = &x; *xPtr = 0.25; *numPtr = *chPtr; num: 65 ‘A’ ch: 0.25 x: numPtr: addr of num addr of ch chPtr: addr of x xPtr:

Pointers and Function Parameters Example: Function to swap the values of two variables: x: 1 y: 2 swap x: 2 y: 1

#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } Bad swap

#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: Bad swap

#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 1 2 a: b: tmp: Bad swap

#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 1 2 a: b: 1 tmp: Bad swap

#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 2 2 a: b: 1 tmp: Bad swap

#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 2 1 a: b: 1 tmp: Bad swap

#include void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 2 1 a: b: 1 tmp: Bad swap

#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } Good swap

#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } 1 2 x: y: Good swap

#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } 1 2 x: y: addr of x addr of y a: b: tmp: Good swap

#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } 1 2 x: y: addr of x addr of y a: b: 1 tmp: Good swap

#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } 2 2 x: y: addr of x addr of y a: b: 1 tmp: Good swap

#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } 2 1 x: y: addr of x addr of y a: b: 1 tmp: Good swap

#include void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } 2 1 x: y: Good swap

Pointers and Function Arguments Change the value of an actual parameter variable. scanf demystified. char ch; intnumx; float numy; scanf(“%c %d %f”, &ch, &numx, &numy);