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

Similar documents
Pointers (1A) Young Won Lim 10/23/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

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

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

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

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

Lecture06: Pointers 4/1/2013

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

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

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

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

CMPE-013/L. Introduction to C Programming

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:

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

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

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

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

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

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.

Variation of Pointers

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

Memory, Data, & Addressing II CSE 351 Spring

Survey. Motivation 29.5 / 40 class is required

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

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

[0569] p 0318 garbage

Laboratory 10 POINTERS I. FUNDAMENTALS

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

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

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

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

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

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

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

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

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.

Pointers, Dynamic Data, and Reference Types

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

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

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

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

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 int a; a can hold an integer &a a a = 100; a holds an integer 100 &a a 100 Pointers 4

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

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 6

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 7

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 8

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 9

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 10

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

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

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 13

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 14

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 15

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 16

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 17

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 18

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 19

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 20

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 21

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 22

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 23

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 24

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 25

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 26

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 27

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 28

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 29

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 30

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

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

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

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

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

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 36

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

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 38

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 39

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 40

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 41

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

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

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

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

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