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

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

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

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

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

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

Applications of Pointers (1A) Young Won Lim 3/21/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 4/24/18

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

Applications of Pointers (1A) Young Won Lim 12/26/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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Lecture06: Pointers 4/1/2013

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

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

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

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

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

Example 1. Young Won Lim 11/17/17

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

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

Example 3. Young Won Lim 11/22/17

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

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

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

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

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

CMPE-013/L. Introduction to C Programming

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

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

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:

a data type is Types

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

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

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

What is an algorithm?

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

Dynamic Memory Allocation and Command-line Arguments

RAM (1A) Young Won Lim 11/12/13

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

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

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

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

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

Data Representation and Storage. Some definitions (in C)

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

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

CS 1613 Lecture 24. Figure 1. Program p01.

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

Variation of Pointers

Memory, Data, & Addressing II CSE 351 Spring

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

Advanced Systems Programming

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

Survey. Motivation 29.5 / 40 class is required

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

[0569] p 0318 garbage

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

Laboratory 10 POINTERS I. FUNDAMENTALS

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

App Note Application Note: State-Driven Control of a dpasp using a Microchip PIC.

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

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

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

Side Effects (3A) Young Won Lim 1/13/18

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24

Pointers, Dynamic Data, and Reference Types

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Transcription:

Pointers (1A)

Coyright (c) 2010-2013 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 OenOffice.

Address and Data in a Memory 10 bits 1K x 8 Memory 8 bits 0x3FF 0x3FE 0x3FD 0x3FC 2 10 = 1024 0x003 0x002 0x001 0x000 HEX 10 bits 8 bits Pointers 3

Variables and their es int a; &a a int * ; & int **q; &q q Pointers 4

Variables with initializations int a; &a a int * = &a; & = &a int **q = &; &q q = & Pointers 5

Pointer variables and ointed es int a; a int * = &a; q int **q = &; &q q Pointers 6

Variables : and * int a; * * a int * = &a; q int **q = &; &q q Pointers 7

Variables : q, *q, and **q int a; *q **q **q a int * = &a; q *q *q int **q = &; &q q Pointers 8

Pass by integer reference void swa(int *, int *q) { int tm; &a a } tm = *; * = *q; *q = tm; &b b int a, b; swa( &a, &b ); Pointers 9

Swaing ointers & &a a &q q &b b & &a a &q q &b b Pointers 10

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

Variables int a; a can hold an integer &a a a = 100; a holds an integer 100 &a a 100 Pointers 12

Pointer Variables int * ; holds an int * ; holds an of a int tye & ointer to int int * ; * holds an integer int * Pointers 13

Pointer to Pointer Variable int ** q; q holds an int ** q; ointer to ointer to int q holds an of a ointer to int tye &q q int * *q; ointer to int *q holds an of a int tye variable q *q int **q; **q holds an integer *q **q int Pointers 14

Pointer Variables int a; int * = & a; &q 0x3CE q = 0x3AB int ** q = & ; & 0x3AB 0x3A0 &a 0x3A0 a 200 &q 0x3CE q 0x3AB *q 0x3A0 **q 200 Pointers 15

Pointer Variables &q q &q 0x3CE q = 0x3AB q *q & 0x3AB 0x3A0 **a a &a 0x3A0 a 200 &q 0x3CE q 0x3AB *q 0x3A0 **q 200 Pointers 16

Pointer Variables int * ; &q 0x3CE q = 0x3AB & 0x3CE = 0x3AB q 0x3AB *q 0x3A0 0x3AB * 200 *q 0x3A0 **q 200 & * 0x3CE 0x3AB 200 &q 0x3CE q 0x3AB *q 0x3A0 **q 200 Pointers 17

Pointer to Pointer Variable int ** q; &q 0x3CE q = 0x3AB q holds an q 0x3AB *q 0x3A0 int ** q; ointer to ointer to int q holds an of a ointer to int tye *q 0x3A0 **q 200 int * *q; ointer to int *q holds an of a int tye variable int **q; **q holds an integer &q 0x3CE q 0x3AB *q 0x3A0 **q 200 int Pointers 18

Interretation of Pointer (1) (int **) q (int *) q (int *) *q (int **) q *q (int *) * *q **q Pointers 19

Interretation of Pointer (2) The of a variable : Address of oerator & content of az ointer : Dereferencing oerator * & & * * * * Pointers 20

Integer Pointer Examles (1) int i; int tye i int * int ** i; qi; int * tye (int *) i i *i i holds an integers i holds an of a int tye int ** tye (int **) qi qi holds an of a ointer to int tye int * tye qi (int *) *qi *qi **qi Pointers 21

Integer Pointer Examles (2) int i; int * i; int ** qi; i holds an integers (int **) qi X X (int *) i holds an of Int tye (float *) qi holds an of Pointer to Int tye (float) Pointers 22

Integer Pointer Examles (3) int i = 200; tyes int ** qi &qi qi = &i int * i = &i; int ** qi = &i; int * i &i i = &i i holds an integers int i &i i =200 i holds an of a int tye qi holds an of a ointer to Int tye *qi = i *i = i *qi = i = &i **qi = *i = i Pointers 23

Variable Declarations int a ; &a a =100 The variable a holds an integer int * ; & 200 The ointer variable holds an, where an integer is stored int * * q ; &q q 30 The ointer variable q holds an, where another is stored, where an integer is stored Pointers 24

Access Data Via Pointer Variables (1) 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 Pointers 25

Access Data Via Pointer Variables (2) 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 Pointers 26

Access Data Via Pointer Variables (3) 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 Pointers 27

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

Array of Pointers (1) 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 * a [4] No. of elements = 4 Tye of each element Pointers 29

Array of Pointers (2) int a [4]; int * b [4]; (int *) a (int * *) b b[0] int a[0] (int *) b[0] a[1] (int *) b[1] b[1] int a[2] (int *) b[2] a[3] (int *) b[3] b[2] int b[3] int Pointers 30

A 2-D Array via a double ointer int c [4] [4]; (int * *) c (int *) c[0] (int *) c[1] (int *) c[2] (int *) c[3] int c[4] [4] 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] c [i][j] (*(c+i))[j] *(*(c+i)+j) Pointers 31

A 2-D array via a single ointer int * =c[0]; int c [4] [4]; c[0] (int * *) c (int *) c[0] (int *) c[1] (int *) c[2] (int *) 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] [i*4+j] Pointers 32

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] Pointers 33

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] Pointers 34

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 Pointers 35

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 Pointers 36

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] Pointers 37

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] = Pointers 38

const tye, const ointer tye (1) const int * ; int * const q ; const int * const r ; Pointers 39

const tye, const ointer tye (2) const int * ; int * const q ; read only integer integer wr integer read only q q wr Pointers 40

const tye, const ointer tye (3) const int * const r ; read only r r wr read only integer integer wr Pointers 41

Pointer Tyes and Associated Data 8 bits increasing char val short val int val c s i char *c; short *s; int *i; Pointers 42

Pointer Tyes 8 bits increasing c s i char *c; short *s; int *i; Pointers 43

Little Endian Examle 8 bits 8 bits &a increasing &c &b c b a int a; short b; char c; the order of definition &b &c a b c increasing &a Pointers 44

int *, short *, char * tye variables int * i; short * s; char * c; i s c Not a sized reresentation Pointers 45

Pointer Variable Assignment 8 bits &c &b &a c b a i s c char * c; short * s; int * i; int a; short b; char c; i = &a; s = &b; c = &c; Pointers 46

Pointer Tye Casting 8 bits &a a *c &a a *s &a *i a c s i char *c; c = (char *) &a short *s; s = (short *) &a int *i; i = (int *) &a Pointers 47

Accessing bytes of a variable 8 bits c+3 *(c+3) c+3 a c+2 c+1 *(c+2) *(c+1) c+2 c+1 &a c &a *(c+0) c c c char *c; c = (char *) &a char *c; c = (char *) &a Pointers 48

32-bit and 64-bit Address 32-bit machine : : 4 bytes 64-bit machine : : 8 bytes 8 bits 8 bits i s c 64-bit machine : 8 bytes 32-bit machine : 4 bytes Pointers 49

64-bit machine : 8-byte i s c char *c; short *s; int *i; Pointers 50

64-bit machine : 8-byte & buses 8 bits char *c; c 8 bits short *s; s 8 bits int *i; i Pointers 51

32-bit machine : 4-byte i s c char *c; short *s; int *i; Pointers 52

64-bit machine : 8-byte and buses 8 bits c char *c; 8 bits s short *s; 8 bits i int *i; Pointers 53

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