CSI-121 Structured Programming Language Lecture 16 Pointers

Slides:



Advertisements
Similar presentations
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Advertisements

CPT: Pointers/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (pointers)
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
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.
Pointers Ethan Cerami Fundamentals of Computer New York University.
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
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.
Pointers CSE 2451 Rong Shi.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
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.
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.
Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
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.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
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 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
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 (Revision). 2 Overview Revision of Pointers Pointers and structs Basic Pointer Arithmetic Pointers and Arrays.
Lecture 5 Pointers 1. Variable, memory location, address, value
Chapter 8 Arrays, Strings and Pointers
Computer Skills2 for Scientific Colleges
“Studying C programming excluding pointers is meaningless.” d0m3z
CSE 220 – C Programming Pointers.
Pointers.
Standard Version of Starting Out with C++, 4th Edition
© 2016 Pearson Education, Ltd. All rights reserved.
POINTERS.
Functions, Part 2 of 2 Topics Functions That Return a Value
Pointers and Pass By Reference
EPSII 59:006 Spring 2004.
INC 161 , CPE 100 Computer Programming
Lecture 6 C++ Programming
CSI-121 Structured Programming Language Lecture 16 Pointers 2
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Programming fundamentals 2 Chapter 2:Pointer
INC 161 , CPE 100 Computer Programming
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
CSI 121 Structured Programming Language Lecture 21 Character Strings
Topics discussed in this section:
Alternate Version of STARTING OUT WITH C++ 4th Edition
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Pointers Problem Solving & Program Design in C Eighth Edition
Programming with Pointers
Functions, Part 2 of 3 Topics Functions That Return a Value
Computer Skills2 for Scientific Colleges
Simulating Reference Parameters in C
Pointers Lecture 1 Thu, Jan 15, 2004.
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
POINTER CONCEPT 4/15/2019.
Arrays and Pointers CSE 2031 Fall May 2019.
POINTER CONCEPT 8/3/2019.
Functions, Part 2 of 3 Topics Functions That Return a Value
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

CSI-121 Structured Programming Language Lecture 16 Pointers CSE1301 Sem 2-2004 CSI-121 Structured Programming Language Lecture 16 Pointers Lecture 16: Pointers

Topics Introduction to pointers Pointers and function parameters

’A’ Memory Address of a Variable ch: char ch = ’A’; CSE1301 Sem 2-2004 Memory Address of a Variable char ch = ’A’; ’A’ 0x2000 ch: The memory address of the variable ch The value of the variable ch Lecture 16: Pointers

’A’ The & Operator &ch Gives the memory address of an object CSE1301 Sem 2-2004 The & Operator Gives the memory address of an object char ch = ’A’; ’A’ 0x2000 &ch yields the value 0x2000 Also known as the “address operator” Lecture 16: Pointers

“conversion specifier” for printing a memory address CSE1301 Sem 2-2004 Example: char ch; printf(“%p”, &ch); “conversion specifier” for printing a memory address Lecture 16: Pointers

Pointers A variable which can store the memory address of another variable 0x2000 chPtr 0x3A15 0x1FFE 0x1FFF 0x2000 0x2001 0x2002 etc ‘B’ ch

Pointers A pointer is a variable Contains a memory address CSE1301 Sem 2-2004 Pointers A pointer is a variable Contains a memory address Points to a specific data type Pointer variables are usually named varPtr Lecture 16: Pointers

Can store an address of variables of type char CSE1301 Sem 2-2004 Example: char* cPtr; cPtr: 0x2004 Can store an address of variables of type char We say cPtr is a pointer to char Lecture 16: Pointers

Pointers and the & Operator CSE1301 Sem 2-2004 Pointers and the & Operator Example: char c = ’A’; char *cPtr; cPtr = &c; Assigns the address of c to cPtr A c: 0x2000 cPtr: 0x2004 0x2000 Lecture 16: Pointers

Notes on Pointers We can have pointers to any data type CSE1301 Sem 2-2004 Notes on Pointers int* numPtr; float* xPtr; Example: We can have pointers to any data type int *numPtr; float * xPtr; Example: The * can be anywhere between the type and the variable Lecture 16: Pointers

Notes on Pointers (cont) CSE1301 Sem 2-2004 Notes on Pointers (cont) You can assign the address of a variable to a “compatible” pointer using the & operator int aNumber; int *numPtr; numPtr = &aNumber; Example: You can print the address stored in a pointer using the %p conversion specifier printf(“%p”, numPtr); Example: Lecture 16: Pointers

The * Operator Allows pointers to access variables they point to CSE1301 Sem 2-2004 The * Operator Allows pointers to access variables they point to Also known as “dereferencing operator” Should not be confused with the * in the pointer declaration Lecture 16: Pointers

Pointers and the  Operator CSE1301 Sem 2-2004 Pointers and the  Operator Example: char c = ’A’; char *cPtr = NULL; Changes the value of the variable which cPtr points to cPtr = &c; *cPtr = ’B’; cPtr: 0x2004 NULL A c: 0x2000 B 0x2000 Lecture 16: Pointers

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

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

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

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

Notes on Pointers (cont) CSE1301 Sem 2-2004 Notes on Pointers (cont) Beware of pointers which are not initialized! int *numPtr; ??? numPtr Lecture 16: Pointers

Notes on Pointers (cont) CSE1301 Sem 2-2004 Notes on Pointers (cont) When declaring a pointer, it is a good idea to always initialize it to NULL (a special pointer constant) int *numPtr = NULL; NULL numPtr Lecture 16: Pointers

Pointers and Function Parameters CSE1301 Sem 2-2004 Pointers and Function Parameters Example: Function to swap the values of two variables swap x: 1 y: 2 x: 2 y: 1 Lecture 16: Pointers

#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; CSE1301 Sem 2-2004 Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; Lecture 16: Pointers

#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; CSE1301 Sem 2-2004 Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; x: 1 y: 2 Lecture 16: Pointers

#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; CSE1301 Sem 2-2004 Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; tmp: a: 1 b: 2 x: 1 y: 2 Lecture 16: Pointers

#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; CSE1301 Sem 2-2004 Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; 1 tmp: a: 1 b: 2 x: 1 y: 2 Lecture 16: Pointers

#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; CSE1301 Sem 2-2004 Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; tmp: 1 a: 2 b: 2 x: 1 y: 2 Lecture 16: Pointers

#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; CSE1301 Sem 2-2004 Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; tmp: 1 a: 2 b: 1 x: 1 y: 2 Lecture 16: Pointers

#include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; CSE1301 Sem 2-2004 Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; tmp: 1 a: 2 b: 1 x: 1 y: 2 Lecture 16: Pointers

#include <stdio.h> void swap2(int* a, int* b) { int tmp; CSE1301 Sem 2-2004 Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; Lecture 16: Pointers

#include <stdio.h> void swap2(int* a, int* b) { int tmp; CSE1301 Sem 2-2004 Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; x: 1 y: 2 Lecture 16: Pointers

#include <stdio.h> void swap2(int* a, int* b) { int tmp; CSE1301 Sem 2-2004 Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; tmp: a: addr of x b: addr of y x: 1 y: 2 Lecture 16: Pointers

#include <stdio.h> void swap2(int* a, int* b) { int tmp; CSE1301 Sem 2-2004 Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; tmp: 1 a: addr of x b: addr of y x: 1 y: 2 Lecture 16: Pointers

#include <stdio.h> void swap2(int* a, int* b) { int tmp; CSE1301 Sem 2-2004 Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; tmp: 1 a: addr of x b: addr of y x: 2 y: 2 Lecture 16: Pointers

#include <stdio.h> void swap2(int* a, int* b) { int tmp; CSE1301 Sem 2-2004 Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; 1 tmp: a: addr of x b: addr of y x: 2 y: 1 Lecture 16: Pointers

#include <stdio.h> void swap2(int* a, int* b) { int tmp; CSE1301 Sem 2-2004 Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; x: 2 y: 1 Lecture 16: Pointers

Pointers and Function Arguments CSE1301 Sem 2-2004 Pointers and Function Arguments Change the value of an actual parameter variable scanf demystified char ch; int numx; float numy; scanf(“%c %d %f”, &ch, &numx, &numy); Lecture 16: Pointers

Reading King Chapter 11 Deitel and Deitel Chapter 7 (7.1-7.4)