Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A2 – Pointers (Revision)

Slides:



Advertisements
Similar presentations
Programming in C Chapter 10 Structures and Unions
Advertisements

Lectures 10 & 11.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Structures Spring 2013Programming and Data Structure1.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
CSE1303 Part A Data Structures and Algorithms Lecture A6 – Dynamic Memory.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A6 – Dynamic Memory.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Topic 2 Pointers CSE1303 Part A, Summer Semester,2002 Data Structures and Algorithms.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A10 – Elementary Algorithms (Revision)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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 Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointers CSE 2451 Rong Shi.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Lecture 17: The Last Puzzle Piece with Functions.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
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.
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers (Revision). 2 Overview Revision of Pointers Pointers and structs Basic Pointer Arithmetic Pointers and Arrays.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
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.
Lecture 5 Pointers 1. Variable, memory location, address, value
Chapter 8 Arrays, Strings and Pointers
CSE 220 – C Programming Pointers.
Pointers.
INC 161 , CPE 100 Computer Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Pointers Problem Solving & Program Design in C Eighth Edition
Programming with Pointers
prepared by Senem Kumova Metin modified by İlker Korkmaz
Pointers.
Chapter 9: Pointers and String
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A2 – Pointers (Revision)

2 Overview Revision of Pointers Pointers and structs Basic Pointer Arithmetic Pointers and Arrays

3 Pointers A pointer is a datatype. You can create variables of this type as you would of other types. Contains a memory address. Points to a specific data type. int *pointer1 = &x; int x; addr of x 1 0x2000 0x9060

4 Pointer Operations aPtr = &object assigns the address of object to aPtr. *aPtr allows access to the object through the pointer. If aPtr points to a structure then (*aPtr).member is equivalent to aPtr-> member Type object; Type* aPtr;

5 Pointers and Function Arguments x: 1 y: 2 swap x: 2 y: 1

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

#include void fakeSwap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; fakeSwap(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: Solution 1 0x2000 0x2010 7

#include void fakeSwap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; fakeSwap(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 1 2 a: b: tmp: Solution 1 0x2000 0x2010 0x2060 0x2038 0x2040 8

#include void fakeSwap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; fakeSwap(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 1 2 a: b: 1 tmp: Solution 1 0x2000 0x2010 0x2060 0x2038 0x2040 9

#include void fakeSwap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; fakeSwap(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 2 2 a: b: 1 tmp: Solution 1 0x2000 0x2010 0x2060 0x2038 0x

#include void fakeSwap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; fakeSwap(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: 2 1 a: b: 1 tmp: Solution 1 0x2000 0x2010 0x2060 0x2038 0x

#include void fakeSwap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } int main() { int x = 1, y = 2; fakeSwap(x, y); printf(“%d %d\n”, x, y); } 1 2 x: y: Solution 1 0x2000 0x

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

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

#include void trueSwap(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; trueSwap(&x, &y); printf(“%d %d\n”, x, y); } 1 2 x: y: addr of x addr of y a: b: tmp: Solution 2 0x2000 0x2010 0x2060 0x2038 0x

#include void trueSwap(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; trueSwap(&x, &y); printf(“%d %d\n”, x, y); } 1 2 x: y: addr of x addr of y a: b: 1 tmp: Solution 2 0x2000 0x2010 0x2060 0x2038 0x

#include void trueSwap(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; trueSwap(&x, &y); printf(“%d %d\n”, x, y); } 2 2 x: y: addr of x addr of y a: b: 1 tmp: Solution 2 0x2000 0x2010 0x2060 0x2038 0x

#include void trueSwap(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } int main() { int x = 1, y = 2; trueSwap(&x, &y); printf(“%d %d\n”, x, y); } 2 1 x: y: addr of x addr of y a: b: 1 tmp: Solution 2 0x2000 0x2010 0x2060 0x2038 0x

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

20 More on Pointers You can print the address stored in a pointer using the %p conversion specifier Example: printf(“%p”, numPtr); scanf() needs to know where to put the value - it needs the address of the variable as it takes pointers as parameters. Example: int i; scanf(“%d”, &i);

21 struct DataBaseRec { }; typedef struct DataBaseRec DataBase; /* Very Large Structure */

22 DataBase readNewEntry(DataBase theDataBase) { return theDataBase; } void printDataBase(DataBase theDataBase) { } /* Some code */ /* Some more code */

23 void readNewEntry(DataBase* dataBasePtr) { } void printDataBase(const DataBase* dataBasePtr) { } /* Some code */ /* Some more code */ (DataBase* Address of Database Database* Database cannot change in this function. const

24 Why Pointers? To modify a variable in a function that is not a global, or a local to that function To save space To save time To use dynamic memory (Lecture A6) Used extensively in linked structures

25 Pointers and Structures struct bookRec{ float price; char name[7]; }; typedef struct bookRec Book Book temp; scanf("%d %s", &temp.price, temp.name); temp

26 Pointers and Structures struct bookRec{ float price; char name[7]; }; typedef struct bookRec Book Book *aPtr; Book temp; aPtr = &temp; scanf("%d %s", &(aPtr->price), aPtr->name); Why do you need the brackets? temp aPtr

27 Pointers and Functions To enable a function to access and change an object. For large structures it is more efficient. Use const specifier whenever a constant is intended.

28 Pointers and Arrays The name array is equivalent to &array[0] pPtr++ increments pPtr to point to the next element of array. pPtr += n increments pPtr to point to n elements beyond where it currently points. pPtr-qPtr equals i-j. Type array[ size ]; Type* pPtr = array + i; Type* qPtr = array + j;

29 Pointers and Arrays (cont) array[0] is equivalent to *array array[n] is equivalent to *(array + n) Type array[ size ]; A normal 1 dimensional array:

30 float array[5]; float* pPtr = array; float* qPtr = NULL; 0x2008 0x2004 array: pPtr: 0x20080x200C0x20100x20140x NULL 0x2000 qPtr: Basic Pointer Arithmetic

31 float array[5]; float* pPtr = array; float* qPtr = NULL; pPtr++; /* pPtr now holds the address: &array[1] */ 0x200C 0x2008 0x2004 array: pPtr: 0x20080x200C0x20100x20140x NULL 0x2000 qPtr:

32 float array[5]; float* pPtr = array; float* qPtr = NULL; pPtr++; /* pPtr = &array[1] */ pPtr += 3; /* pPtr now hold the address: &array[4] */ 0x2018 0x2008 0x2004 array: pPtr: 0x20080x200C0x20100x20140x NULL 0x2000 qPtr:

33 float array[5]; float* pPtr = array; float* qPtr = NULL; pPtr++; /* pPtr = &array[1] */ pPtr += 3; /* pPtr = &array[4] */ qPtr = array + 2; /*qPtr now holds the address &array[2]*/ 0x2018 0x2008 0x2004 array: pPtr: 0x20080x200C0x20100x20140x x2010 0x2000 qPtr:

34 float array[5]; float* pPtr = array; float* qPtr = NULL; pPtr++; /* pPtr = &array[1] */ pPtr += 3; /* pPtr = &array[4] */ qPtr = array + 2; /* qPtr = &array[2] */ printf(“%d\n”, pPtr-qPtr); 0x2018 0x2008 0x2004 array: pPtr: 0x20080x200C0x20100x20140x x2010 0x2000 qPtr:

char* strcpy(char* s, char* t) { int i = 0; while (t[i] != 0) { s[i] = t[i]; i++; } s[i] = '\0'; return s; } char*strcpy(char* s, char* t) { char* p = s; while (*p != 0) { *p = *t; p++; t++; } return s; } 35

36 Revision Pointers, operations, structures, arguments, arithmetic and arrays –address operator ( & ), and dereferencing operator ( * ) –structure pointer operator ( -> ) –Look at “swap” example Revision: Reading Kruse - Appendix C.6 Deitel & Deitel - Chapter 7 King - Chapter 11, 12 Langsam - Chapter 1.1 Standish – Chapter 2.3 Kernighan and Ritchie - Chapter 5.1 – 5.10, 6.2 Preparation Next lecture: Stacks Read to in Kruse et al.