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

Similar documents
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 10/18/17

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

Pointers (1A) Young Won Lim 1/14/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

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

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

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

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

Pointers (1A) Young Won Lim 3/5/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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Example 1. Young Won Lim 11/17/17

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

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

Example 2. Young Won Lim 11/24/17

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

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

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

Example 3. Young Won Lim 11/22/17

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A Sudoku Solver (1A) Richard Bird Implementation. Young Won Lim 11/15/16

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

Side Effects (3B) Young Won Lim 11/27/17

Tiny CPU Data Path (2B) Young Won Lim 5/2/16

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

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

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

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

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

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

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

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

Polymorphism Overview (1A) Young Won Lim 2/20/18

Side Effects (3B) Young Won Lim 11/20/17

Side Effects (3B) Young Won Lim 11/23/17

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:

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

Haskell Overview II (2A) Young Won Lim 8/9/16

State Monad (3D) Young Won Lim 9/25/17

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

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

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

Libraries (1A) Young Won Lim 6/5/17

Methods (2A) Young Won Lim 10/14/14

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

Structure (1A) Component

CORDIC Fixed Point Simulation. Young Won Lim 2/28/12

HW / SW Implementation Overview (0A) Young Won Lim 7/16/16

Monad (1A) Young Won Lim 6/9/17

CSC231 C Tutorial Fall 2018 Introduction to C

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Haskell Overview IV (4A) Young Won Lim 10/13/16

Pthread (9A) Pthread

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

Haskell Overview III (3A) Young Won Lim 10/4/16

Fundamental of Programming (C)

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

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Background Functions (1C) Young Won Lim 4/3/18

FPGA Configuration (1C) Young Won Lim 12/1/13

Transcription:

Pointers (1A)

Copyright (c) 2010-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy 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 produced by using OpenOffice.

Array of Pointers (1) int a [4]; int * b [4]; Array name a holds the starting address int a [4] No. of elements = 4 Type of each element Array name b holds the starting address int * a [4] No. of elements = 4 Type of each element Pointers 3

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 4

A 2-D Array via a double pointer 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 5

A 2-D array via a single pointer int * p =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] p[i*4+j] Pointers 6

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 7

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 8

Pointer to array (1) int a [4]; int a [4] (int []) a int (*p) [4] pointer to the array of 4 elements a[0] a[1] a[2] a[3] int m ; an integer variable int *n ; a pointer variable int func (int a, int b) ; a prototype int (* fp) (int a, int b) ; a function's type int * fp (int a, int b) ; function pointer Pointers 9

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

Pointer to array (3) &p int (*p) [4] ; int c[4] [4] (int (*) []) p p = c (*p) [ i ][ j ]; a 2-d array with 4 rows and 4 columns (int (*) []) p (int []) (int []) (int []) (int []) c *p c[0] c[1] c[2] c[3] *(p+0) *(p+1) *(p+2) *(p+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 11

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

const type, const pointer type (1) const int * p; int * const q ; const int * const r ; Pointers 13

const type, const pointer type (2) const int * p; int * const q ; read only integer integer wr integer p address read only q q address wr Pointers 14

const type, const pointer type (3) const int * const r ; read only r r address wr read only integer integer wr Pointers 15

Pointer Types and Associated Data 8 bits data data data increasing address char val short val int val pc ps pi address address address char *pc; short *ps; int *pi; Pointers 16

Pointer Types 8 bits data data data increasing address pc ps pi address address address char *pc; short *ps; int *pi; Pointers 17

Little Endian Example 8 bits 8 bits data data &a increasing address &c &b c b a int a; short b; char c; the order of definition &b &c a b c increasing address &a Pointers 18

int *, short *, char * type variables int * pi; short * ps; char * pc; pi ps pc address Not a sized representation Pointers 19

Pointer Variable Assignment 8 bits &c &b &a data c b a pi ps pc address char * pc; short * ps; int * pi; int a; short b; char c; pi = &a; ps = &b; pc = &c; Pointers 20

Pointer Type Casting 8 bits data data data &a a *pc &a a *ps &a *pi a pc ps pi address address address char *pc; pc = (char *) &a short *ps; ps = (short *) &a int *pi; pi = (int *) &a Pointers 21

Accessing bytes of a variable 8 bits data data pc+3 *(pc+3) pc+3 a pc+2 pc+1 *(pc+2) *(pc+1) pc+2 pc+1 &a pc &a *(pc+0) pc pc pc address address char *pc; pc = (char *) &a char *pc; pc = (char *) &a Pointers 22

32-bit and 64-bit Address 32-bit machine : address : 4 bytes 64-bit machine : address : 8 bytes 8 bits 8 bits pi ps pc 64-bit machine address : 8 bytes 32-bit machine address : 4 bytes Pointers 23

64-bit machine : 8-byte address pi ps pc char *pc; short *ps; int *pi; Pointers 24

64-bit machine : 8-byte address & data buses 8 bits char *pc; pc 8 bits short *ps; ps 8 bits int *pi; pi Pointers 25

32-bit machine : 4-byte address pi ps pc char *pc; short *ps; int *pi; Pointers 26

64-bit machine : 8-byte address and data buses 8 bits pc char *pc; 8 bits ps short *ps; 8 bits pi int *pi; Pointers 27

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 Express, I. K. Chun