CSE 220 – C Programming Pointers.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
1 CSE1301 Computer Programming Lecture 16 Pointers.
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 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
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.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures 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.
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 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Object-Oriented Programming in C++
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Functions & Pointers in C Jordan Erenrich
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.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
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.
Structures, Unions, Enumerations
Computer Science 210 Computer Organization
C Primer.
Introduction to Programming Using C
Functions and Pointers
Pointers in C.
Pointers.
INC 161 , CPE 100 Computer Programming
Lecture 6 C++ Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Programming fundamentals 2 Chapter 2:Pointer
Pointers.
Functions and Pointers
INC 161 , CPE 100 Computer Programming
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Computer Science 210 Computer Organization
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Programming in C Pointer Basics.
Pass by Reference.
Pointers.
Chapter 16 Pointers and Arrays
Simulating Reference Parameters in C
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
ECE Application Programming
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Exercise Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
15213 C Primer 17 September 2002.
Presentation transcript:

CSE 220 – C Programming Pointers

Pointer Variables x p In memory: each byte has an address … 2001 2002 2003 2100 01011101 01000101 1101000 x p In memory: each byte has an address Pointers: used to store the address Store the address of x in pointer variable p: p points to x

Pointer Declaration a in an int b is a pointer to an int Add a * before the variable name to indicate a pointer variable int *p; //p can only point to an integer double *q; //q can only point to a double char *r; //r can only point to a character Pointer variables can appear with other declarations: int a, *b, c[10], *q; int * b, a; a in an int b is a pointer to an int

Address Operator: & int i, *p; p = &i; p does not point to any particular place Must initialize p before using it p = &i; Assign address of i to p using address operator & p is a pointer to i *p is the value that p points to Declare and initialize in one step (optional material): int i, *p = &i; //i must be declared first Combine declaration and initialization

Which of these variables are pointers? int x, *y; int *z; char *a; x = 4; y = &x; z = y; loc. 124 125 126 127 128 129 130 value 4 ? x z a y x y z a

int i, *p; p = &i; //address of i: 2002 Random value in memory Random value in memory 1 … 2002 2100 427 1005 i p 2 … 2002 2100 427

Indirection Operator: * The indirection operator * is used to access the value pointed to by a pointer: Must initialize p before using it int i = 11; int *p = &i; printf(“%d\n”, *p); //prints 11 printf(“%p\n”, p); // 0x7fffffffda6c

Examples &i: pointer to i int i, j, *p; j = *&i; i = 3; int *p = &i; i = 4; printf("%d\n", *p); &i: pointer to i *(&i): value pointed to by the pointer *&i same as i p points to i, so it prints the current value of i

Examples int i = 3, *p = &i; *p = 4; printf("%d\n", i); printf("%d\n", *p); int *q; printf("%d\n", *q); *q = 1; Line 2: Assigns 4 to the value pointed to by p, so assigns 4 to i Line 3: Prints 4 Line 4: Also, prints 4 q is uninitialized, does not point to any particular location Undefined behavior

What does this code output? int a, *b; int c, *d; a = 4; c = 5; b = &a; d = &c; printf("%d%d%d%d", a, *b, c, *d); Error 4545 4455 I don't know

Pointer Assignment p a q int a, b, *p, *q; p = &a; //Copy the address of a into p q = p; //Copy the content of p into q 3002 p a 3002 q 3002

Pointer Assignment 5 p a q *p = 5; //Change the value pointed to by p to 5 printf("%d\n", *q); p 5 a q

Pointer Assignment 5 5 7 7 q = p; int a = 5, *p = &a; int b = 7, *q = &b; q = p; Copies the content of p to the content of q p 5 5 a p a 7 7 q b q b

Pointer Assignment 5 5 7 5 *q = *p; p a p a q b q b int a = 5, *p = &a; int b = 7, *q = &b; *q = *p; Copies value pointed to by p to value pointed to by q p 5 5 a p a 7 5 q b q b

What does this code output? int a, *b, c, *d; a = 4; c = 5; b = &a; d = &c; *d = 6; printf("%d%d%d%d", a, *b, c, *d); 4455 4466 4465 I don't know

Pointers as Arguments a = 7 a = 21 C passes arguments by value What if we want to modify the value? void triple(int a) { a = 3*a; } … int a = 7; triple(a); printf(“a = %d\n”, a); int triple(int a) { return 3*a; } … int a = 7; a = triple(a); printf(“a = %d\n”, a); a = 7 a = 21

Pointers as Arguments What if we want the function to modify multiple values? Solution: pass a pointer to the value

Pointers as Arguments void swap(int a, int b) { int temp = a; a = b; b = temp; } int x = 10, y = 20; swap(x, y); printf(“x = %d, y = %d”, x, y); After calling the function swap, the values of x and y remain the same. x = 10, y = 20

Pointers as Arguments 10 20 10 a x y b temp void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int x = 10, y = 20; swap(&x, &y); /* swap expects a pointer: so pass &x instead of x, &y instead of y */ a 10 x 20 b y 10 temp

Pointers as Arguments 20 20 10 a x y b temp void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int x = 10, y = 20; swap(&x, &y); /* swap expects a pointer: so pass &x instead of x, &y instead of y */ a 20 x 20 b y 10 temp

Pointers as Arguments 20 10 10 a x y b temp void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int x = 10, y = 20; swap(&x, &y); /* swap expects a pointer: so pass &x instead of x, &y instead of y */ a 20 x 10 b y 10 temp

What does this code output? void increment(int * x) { (*x)++; } ... int a = 4; increment(&a); printf("%d", a); 5 Error 4 I don't know

Scanf revisited int x; scanf(“%d”, &x); Scanf expects a pointer, so the address of x is passed. x is passed by reference, the change to x persists after the function returns.

Pointers as return values C allows functions to return a pointer int *max(int *a, int *b) { if (*a > *b) return a; else return b; } int *p, i, j; … p = max(&i, &j);

Pitfalls Apply indirection to uninitialized pointer int *p; int x = *p; //p pointing to a random location Fail to pass a pointer to a function when a pointer is expected void multiply(int a, int b, int *result); … int x = 10, y = 20, z; multiply(x, y, &z);

Pitfalls Return a pointer to a function variable int * multiply(int a, int b) { int result = a*b; return &result; } … int *z; z = multiply(7, 10); This location is destroyed after the function concludes. z result 70

What does this code output? void double(int * x, int * y) { *y = *x * 2; } ... int a = 4, b; increment(&a, b); printf("%d", b); 5 Error 4 I don't know