Applications of Pointers (1A) Young Won Lim 3/21/18

Similar documents
Applications of Pointers (1A) Young Won Lim 3/14/18

Applications of Pointers (1A) Young Won Lim 3/31/18

Applications of Pointers (1A) Young Won Lim 4/11/18

Applications of Pointers (1A) Young Won Lim 2/27/18

Applications of Pointers (1A) Young Won Lim 1/5/18

Applications of Pointers (1A) Young Won Lim 4/24/18

Pointers (1A) Young Won Lim 10/23/17

Pointers (1A) Young Won Lim 10/18/17

Pointers (1A) Young Won Lim 12/4/17

Pointers (1A) Young Won Lim 11/1/17

Applications of Pointers (1A) Young Won Lim 12/26/17

Applications of Arrays (1A) Young Won Lim 2/11/17

Applications of Arrays (1A) Young Won Lim 3/15/17

Arrays (1A) Young Won Lim 12/4/17

Arrays (1A) Young Won Lim 1/27/17

Pointers (1A) Young Won Lim 1/9/18

Pointers (1A) Young Won Lim 1/5/18

Pointers (1A) Young Won Lim 3/5/18

Overview (1A) Young Won Lim 9/14/17

Overview (1A) Young Won Lim 9/9/17

Applications of Structures (1A) Young Won Lim 12/8/17

Overview (1A) Young Won Lim 9/25/17

Applications of Structures (1A) Young Won Lim 12/4/17

Pointers (1A) Young Won Lim 1/14/18

Pointers (1A) Young Won Lim 2/6/18

Pointers (1A) Young Won Lim 2/10/18

Pointers (1A) Young Won Lim 1/22/18

Function Overview (1A) Young Won Lim 10/23/17

Type (1A) Young Won Lim 2/17/18

Memory Arrays (4H) Gate Level Design. Young Won Lim 3/15/16

Structures (1A) Young Won Lim 12/4/17

Structure (1A) Young Won Lim 7/30/13

Applications of Array Pointers (1A) Young Won Lim 11/22/18

File (1A) Young Won Lim 11/25/16

Variables (2D) Young Won Lim 3/28/18

Structures (1A) Young Won Lim 11/8/16

Arrays and Strings (2H) Young Won Lim 3/7/18

Number System (1A) Young Won Lim 7/7/10

Polymorphism (1A) Young Won Lim 8/22/13

Polymorphism (1A) Young Won Lim 8/15/13

Memory (1A) Young Won Lim 9/7/17

Pointers (2G) Young Won Lim 3/7/18

Example 1. Young Won Lim 11/17/17

Example 1 : using 1-d arrays. Young Won Lim 12/13/17

Example 2. Young Won Lim 11/24/17

Example 3 : using a structure array. Young Won Lim 11/25/17

Lecture06: Pointers 4/1/2013

ELF (1A) Young Won Lim 10/22/14

Expressions (2E) Young Won Lim 3/10/18

Expressions (2E) Young Won Lim 4/9/18

Example 3. Young Won Lim 11/22/17

Program Structure (2A) Young Won Lim 5/28/18

Program Structure (2A) Young Won Lim 3/8/18

ARM Assembly Exercise (1B) Young Won Lim 7/16/16

Accessibility (1A) Young Won Lim 8/22/13

Binary Search Tree (2A) Young Won Lim 5/17/18

Definition. Pointers. Outline. Why pointers? Definition. Memory Organization Overview. by Ziad Kobti. Definition. Pointers enable programmers to:

Class (1A) Young Won Lim 9/8/14

Pointers and Memory Allocation p. 1. Brooklyn College. Michael Lampis. CISC 3130 Notes. Pointers and Memory Allocation

Algorithms Bubble Sort (1B) Young Won Lim 4/5/18

Binary Search Tree (3A) Young Won Lim 6/2/18

Structures (2I) Young Won Lim 4/17/18

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Class (1A) Young Won Lim 11/20/14

Binary Search Tree (3A) Young Won Lim 6/4/18

Binary Search Tree (3A) Young Won Lim 6/6/18

Structures (2I) Young Won Lim 3/7/18

CMPE-013/L. Introduction to C Programming

Functions (4A) Young Won Lim 3/16/18

Storage Allocation CSE 143. Pointers, Arrays, and Dynamic Storage Allocation. Pointer Variables. Pointers: Review. Pointers and Types

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

Day08 A. Young W. Lim Mon. Young W. Lim Day08 A Mon 1 / 27

ELF (1A) Young Won Lim 3/24/16

Eulerian Cycle (2A) Young Won Lim 4/26/18

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Functions (4A) Young Won Lim 5/8/17

Algorithms Overview (1A) Young Won Lim 3/29/18

Arrays. Young W. Lim Mon. Young W. Lim Arrays Mon 1 / 17

CS 1613 Lecture 24. Figure 1. Program p01.

Example: Runtime Memory Allocation: Example: Dynamical Memory Allocation: Some Comments: Allocate and free dynamic memory

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

State Monad Example (3H) Young Won Lim 2/16/18

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Arrays. Young W. Lim Wed. Young W. Lim Arrays Wed 1 / 19

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

[0569] p 0318 garbage

a data type is Types

Monad Background (3A) Young Won Lim 11/20/17

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

What is an algorithm?

Preprocessing (2K) Young Won Lim 3/7/18

Pointers. Memory. void foo() { }//return

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Algorithms (7A) Young Won Lim 4/10/17

Example: Pointer Basics

Structure (1A) Component

Transcription:

(1A)

Coyright (c) 2010-2018 Young W. Lim. Permission is granted to coy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version ublished by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A coy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was roduced by using LibreOffice.

Variables and their es data int a; &a a int * ; & int **q; &q q 3

Initialization of Variables data int a = 100; &a a = 100 int * = &a; & = &a int **q = &; &q q = & 4

Traditional arrow notations data data &a a = 100 &a a = 100 & = &a & = &a &q q = & &q q = & 5

Pointed es :, q data int a; a int * = &a; q int **q = &; &q q = &a q = & 6

Dereferenced Variables : * data int a; * * a int * = &a; & int **q = &; 7

Dereferenced Variables : * int a; int * = &a; int **q = &; Address assignment = &a &a *() *(&a) * a Variable aliasing * a Relations after assignment 8

Dereferenced Variables : *q, **q data int a; *q **q **q a int * = &a; q *q *q int **q = &; &q q 9

Dereferenced Variables : *q, **q int a; int * = &a; Address assignment = &a Variable aliasing * a int **q = &; q = & *q **q a q & *(q) *(&) * q **q * **q a Relations after assignment 10

Two more ways to access a : *, **q &a a * *q **q & & q *q &q q &q q &q q a * a **q a 11

Two more ways to access a : *, **q data &a & a * **q &q q 1) Read / Write a 2) Read / Write * 3) Read / Write **q 12

Variables int a; a can hold an integer &a data a a = 100; a holds 100 &a data a 100 13

Pointer Variables int * ; can hold an int * ; holds an of a int tye data & ointer to int int * ; * holds a int tye data int * 14

Pointer to Pointer Variable int ** q; q holds an int ** q; q holds an of a ointer to int tye data ointer to ointer to int &q q int * *q; ointer to int *q holds an of a int tye data q *q int **q; **q holds a int tye data *q **q int 15

Pointer Variables Examles int a = 200; &a 0x3A0 a data 200 int * = & a; & 0x3AB 0x3A0 int ** q = & ; &q 0x3CE q = 0x3AB &q 0x3CE q 0x3AB *q 0x3A0 **q 200 16

Pointer Variable with an arrow notation data * &a 0x3A0 a data 200 & & 0x3AB 0x3A0 using an arrow notation & * 0x3AB 0x3A0 200 17

Pointer Variable q with an arrow notation data data *q **q &a 0x3A0 a 200 q *q & 0x3AB 0x3A0 &q q &q 0x3CE q = 0x3AB using an arrow notation &q q 0x3CE 0x3AB *q 0x3A0 **q 200 18

The tye view oint of ointers data (int *) (int **) Tyes 19

The different view oints of ointers **q *q (int *) *q q (int **) q &q Tyes Variables Addresses 20

Single and Double Pointer Examles (1) int a ; int * ; int **q ; a * a, *, and **q: int variables q q *q *q **q 21

Single and Double Pointer Examles (2) int a ; int * ; int * *q ; a * and *q : int ointer variables (singleointers) q q *q *q **q 22

Single and Double Pointer Examles (3) int a ; int * ; int ** q ; a * q : double int ointer variables q q *q *q **q 23

Values of double ointer variables int **, **q ; (int **) (int **) X X = q; (int *) (float *) (float) 24

Variable Declarations int a ; &a a =100 The variable a holds an integer data int * ; & 200 The ointer variable holds an, at this, an integer data is stored int * * q ; &q q *q 30 The ointer variable q holds an, at the q, another *q is stored, at the *q, an integer data **q is stored 25

Access Data Via Pointer Variables (1) int a ; &a a =100 Direct Access &a value a integer 26

Access Data Via Pointer Variables (2) int * ; & *=200 Indirect Access Dereference Oerator * the content of the ointed location & value *(&) * * integer 27

Access Data Via Pointer Variables (3) int * * q ; &q q q *q *q **q=30 Double Indirect Access &q value q Dereference Oerator * the content of the ointed location *(&q) *q q *q Dereference Oerator * the content of the ointed location *q **q *q **q integer 28

Access Data Via Pointer Variables (4) int a ; &a a =100 Direct Access &a value a integer int * ; & *=200 Indirect Access Dereference Oerator * the content of the ointed location value & * integer int * * q ; &q q q *q *q **q=30 Double Indirect Access Dereference Oerator * the content of the ointed location value &q q q *q *q **q integer 29

Access Data Via Pointer Variables (5) * int a &a a =100 *(&a) = a * * int * & *=200 *(&) = *() = * * * * int * * q &q q q *q *q **q=30 *(&q) = q *(q) = *q *(*q) = **q 30

Swaing ointers - ass by reference - double ointers 31

Swaing integer ointers & = &a & = &b &q q = &b &q q = &a a = 111 a = 111 b = 222 b = 222 32

Swaing integer ointers & = &a & = &b &q q = &b &q q = &a int *, *q; swa_ointers( &, &q ); swa_ointers( int **, int ** ); function call function rototye 33

Pass by integer ointer reference void swa_ointers (int **m, int **n) { int* tm; } tm = *m; *m = *n; *n = tm; int ** m int * *m int ** n int * *n int * tm int a, b; int *, *q; =&a, q=&b; swa_ointers( &, &q ); 34

Array of Pointers 35

Array of Pointers int a [4]; int * b [4]; Array name a holds the starting int a [4] No. of elements = 4 Tye of each element Array name b holds the starting int * b [4] No. of elements = 4 Tye of each element 36

Array of Pointers variable view int a [4]; int * b [4]; a b b[0] *b[0] = 11 a[0] = 11 b[0] a[1] = 22 b[1] b[1] *b[1] = 22 a[2] = 33 b[2] a[3] = 44 b[3] b[2] *b[2] = 33 b[3] *b[3] = 44 37

Array of Pointers tye view int a [4]; int * b [4]; (int *) (int * *) (int *) (int *) (int *) (int *) 38

2-d Arrays 39

A 2-D Array int c [4] [4]; c c[0] c[1] c[2] c[3] c[0] c[1] c[2] c[3] c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] c[2][2] c[2][3] c[3][0] c[3][1] c[3][2] c[3][3] int c[4] [4] 40

A 2-D Array int c [4] [4]; (int * *) (int *) (int *) (int *) (int *) c[0] c[1] c[2] c[3] int c[4] [4] 41

A 2-D Array via a double ointer int c [4] [4]; (c [i])[j] (*(c+i))[j] *(*(c+i)+j) (c [I]) = (*(c+i)) (_)[j] = *((_)+j) int c[4] [4] 42

A 2-D Array int c [4] [4]; (int * *) (int *) (int *) (c+i) (int *) (int *) c[0] c[1] c[2] *(c+i)+j c[3] (c [i])[j] (*(c+i))[j] *(*(c+i)+j) 43

A 2-D array via a single ointer int c [4] [4]; c[0] c c[0] c[1] c[2] c[3] c[i][j] c[1] c[2] c[3] c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] c[2][2] c[2][3] c[3][0] c[3][1] c[3][2] c[3][3] 0=[0*4+0] 1=[0*4+1] 2=[0*4+2] 3=[0*4+3] 4=[1*4+0] 5=[1*4+1] 6=[1*4+2] 7=[1*4+3] 8=[2*4+0] 9=[2*4+1] 10=[2*4+2] 11=[2*4+3] 12=[3*4+0] 13=[3*4+1] 14=[3*4+2] 15=[3*4+3] 44

A 2-D array via a single ointer int c [4] [4]; c[0] c c[0] c[1] c[2] c[3] c[i][j] c[1] c[2] c[3] [0*4+0] [0*4+1] [0*4+2] [0*4+3] [1*4+0] [1*4+1] [1*4+2] [1*4+3] [2*4+0] [2*4+1] [2*4+2] [2*4+3] [3*4+0] [3*4+1] [3*4+2] [3*4+3] int * =c[0]; [i*4+j] 45

2-D Array Dynamic Memory Allocation (1) int ** d ; d = (int **) malloc (4 * size of (int *)); for (i=0; i<4; ++i) d[i] = (int *) malloc(4 * sizeof); (int **) d (int *) (int *) (int *) (int *) d[0] d[1] d[2] d[3] 46

2-D Array Dynamic Memory Allocation (2) int ** d ; d = (int **) malloc (4 * size of (int *)); for (i=0; i<4; ++i) d[i] = (int *) malloc(4 * sizeof); &d (int **) d (int *) (int *) (int *) (int *) d[0] d[1] d[2] d[3] d[0][0] d[0][1] d[0][2] d[0][3] d[1][0] d[1][1] d[1][2] d[1][3] d[2][0] d[2][1] d[2][2] d[2][3] d[3][0] d[3][1] d[3][2] d[3][3] 47

Pointer to Arrays 48

Pointer to array (1) int a [4]; int a [4] (int []) a int (*) [4] ointer to the array of 4 elements a[0] a[1] a[2] a[3] int m ; an integer variable int *n ; a ointer variable int func (int a, int b) ; a rototye int (* f) (int a, int b) ; a function's tye int * f (int a, int b) ; function ointer 49

Pointer to array (2) int (*) [4] ; (int (*) []) int a [4] (int []) * a * = a (*) = a &(*) = &a = &a sizeof()= 4 bytes sizeof(*)= 16 bytes a[0] a[1] a[2] a[3] an array with 4 integer elements 50

Pointer to array (3) & int (*) [4] ; int c[4] [4] (int (*) []) = c (*) [ i ][ j ]; a 2-d array with 4 rows and 4 columns (int (*) []) (int []) (int []) (int []) (int []) c * c[0] c[1] c[2] c[3] *(+0) *(+1) *(+2) *(+3) c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] c[2][2] c[2][3] c[3][0] c[3][1] c[3][2] c[3][3] 51

Pointer to array (4) int c [4][4]; int (*) [4]; = c; func(,... ); void func(int (*x)[4],... ) { } x[r][c] = void func(int x[ ][4],... ) { } x[r][c] = 52

References [1] Essential C, Nick Parlante [2] Efficient C Programming, Mark A. Weiss [3] C A Reference Manual, Samuel P. Harbison & Guy L. Steele Jr. [4] C Language Exress, I. K. Chun