Topic 2 Pointers CSE1303 Part A, Summer Semester,2002 Data Structures and Algorithms.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

Structures Spring 2013Programming and Data Structure1.
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.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
CPT: Pointers/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (pointers)
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.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A2 – Pointers (Revision)
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.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
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.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
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.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
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.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
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.
Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
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.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
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.
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 IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
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.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
CSE 220 – C Programming Pointers.
UNIT 5 C Pointers.
Pointers.
INC 161 , CPE 100 Computer Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Computer Science 210 Computer Organization
Lecture 18: The Elegant, Abstract World of Computing
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Pointers Problem Solving & Program Design in C Eighth Edition
Pointers.
C++ Pointers and Strings
Exercise Arrays.
Lecture 14: Problems with Lots of Similar Data
C++ Pointers and Strings
Presentation transcript:

Topic 2 Pointers CSE1303 Part A, Summer Semester,2002 Data Structures and Algorithms

Overview Revision of Pointers Basic Pointer Arithmetic Pointers and Arrays

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

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;

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

#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

#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

#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

#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 0x2040

#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 0x2040

#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

#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

#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 0x2010

#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 0x2040

#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 0x2040

#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 0x2040

#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 0x2040

#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 0x2010

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);

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

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

void readNewEntry(DataBase* dataBasePtr) { } void printDataBase(const DataBase* dataBasePtr) { } /* Some code */ /* Some more code */

void readNewEntry(DataBase* dataBasePtr) { } void printDataBase(const DataBase* dataBasePtr) { } /* Some code */ /* Some more code */ (DataBase* Address of Database Database*

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

int readstudent(Student *student) { printf(“Enter a name: \n”); if (scanf(“%s”, student->name) == 1) { printf(“Enter a mark: \n); if (scanf(“%d”, &(student->mark)) == 1) { return 1; } return 0; } Why do you need the brackets?

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.

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;

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:

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

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:

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:

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:

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++; } return s; } char* strcpy(char* s, char* t) { char* p = s; while (*p != 0) { *p = *t; p++; t++; } return s; }

Revision Pointers Pointer Operations –address operator (&), and dereferencing operator ( * ) Pointers and Structures. –structure pointer operator ( -> ) Pointers and Function Arguments. –Look at “swap” example Pointer Arithmetic Pointers and Arrays

Next Lecture Stacks –abstract data type –main operations –implementation Preparation Read to in Kruse et al.