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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Example 2. Young Won Lim 11/24/17

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

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

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

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

Example 1. Young Won Lim 11/17/17

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

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

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

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

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

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

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

Example 3. Young Won Lim 11/22/17

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

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 Fri. Young W. Lim Access Fri 1 / 18

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

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

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

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

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

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

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

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

LOÏC CAPPANERA. 1. Memory management The variables involved in a C program can be stored either statically or dynamically.

Two Dimensional Arrays

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

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

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

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

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

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

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

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

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

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Pointers, Arrays and Parameters

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

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

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

More Arrays. Last updated 2/6/19

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

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

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

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

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

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

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

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

Warmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2

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

Background Functions (1C) Young Won Lim 12/6/17

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

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

Transcription:

Appliations of Array Pointers (1A)

Copyright () 2010-2018 Young W. Lim. Permission is granted to opy, distribute and/or modify this doument under the terms of the GNU Free Doumentation Liense, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Setions, no Front-Cover Texts, and no Bak-Cover Texts. A opy of the liense is inluded in the setion entitled "GNU Free Doumentation Liense". Please send orretions (or suggestions) to youngwlim@hotmail.om. This doument was produed by using LibreOffie.

Pointer to Multi-dimensional Arrays of Array Pointers 3

Integer pointer type (int (*)[4]) type an point only to int [4] type an int array name a pointer to a pointer (int **) *) (int *) a pointer to an int (int [4]) *) an int array name a pointer to a 1-d array (int (*)[4]) 1-d array pointer int [4] = (int [ ] int *) eah of these types points to an int type data of Array Pointers 4

Series of array pointers a type view a pointer to a 1-d array a pointer to a 1-d array a pointer to a 1-d array (int (*)[4]) (int (*)[4]) (int (*)[4]) 1-d array pointers (int [4]) *) the name of a 1-d int array (int [4]) *) the name of a 1-d int array (int [4]) *) the name of a 1-d int array of Array Pointers 5

Series of array pointers 1-d arrays a, b, int a[4]; int (*r); int b[4]; int [4]; (int *) r taking no memory loations a b a a a a b b b b assume that array a, b, and are ontiguous of Array Pointers 6

Series of array pointers array pointers p1, p2, p3 int (*p1)[4]; int (*p2)[4]; int (*p3)[4]; assignment p1 = &a p2 = &b p3 = & a pointer to a 1-d array a pointer to a 1-d array a pointer to a 1-d array 1-d array pointers p1 p2 p3 1-d arrays taking no memory loations a b a a a a b b b b assume that array p1, p2, and p3 are ontiguous assume that array a, b, and are ontiguous of Array Pointers 7

Series of array pointers 1-d arrays via p1, p2, p3 a pointer to a 1-d array a pointer to a 1-d array a pointer to a 1-d array equivalene (*p1) p1 a (*p2) p2 b (*p3) p3 p1 p2 p3 1-d array pointers taking no memory loations p1 p2 p3 p1 p1 p1 p1 p2 p2 p2 p2 assume that array a, b, and are ontiguous of Array Pointers 8

Series of array pointers a variable view int a[4]; int (*p1)[4]; int (*r); int b[4]; int (*p2)[4]; int [4]; int (*p3)[4]; assignment p1 = &a p2 = &b p3 = & a pointer to a 1-d array a pointer to a 1-d array a pointer to a 1-d array equivalene (*p1) p1 a (*p2) p2 b (*p3) p3 p1 p2 p3 1-d array pointers (int *) r taking no memory loations a b a a a a b b b b assume that array a, b, and are ontiguous in the memory of Array Pointers 9

Extending a 1-d pointer array a variable view int (*q)[4][4]; (int (*)[4]) q (int *) [4]) q an array of 1-d arrays q+1 (int *) [4]) q q+2 (int *) [4]) q assignment q = a q = b q = equivalene q *(q+0) a q *(q+1) b q *(q+2) taking onseutive memory loations 1-d array names assume arrays a, b, are onseutive q q q q q q q q q q q q of Array Pointers 10

1-d array pointer to onseutive 1-d arrays int (*p)[4]; a pointer to a pointer array p 1-d array pointer assignment p = &a equivalene *(p+0) p a *(p+1) p b *(p+2) p *(p+2) p d if arrays a, b,, d are onseutive p+0 p+1 p+2 p+3 p p p p p p p p p p p p p p p p p p p p of Array Pointers 11

A 2-d array and its sub-arrays array name int [4][4]; : the 2-d array name the 2-d array starting address the 1-d array pointer points to its 1 st 1-d sub-array ompilers do not alloate s memory loation a 2-d array with 4 rows and 4 olumns +1 +2 +3 of Array Pointers 12

A 2-d array and its sub-arrays subarray names int [4][4]; [i] the 1-d array name the 1-d array starting address the 0-d array pointer points to its salar integer the 1 st 1-d subarray name the 2 nd 1-d subarray name the 3 rd 1-d subarray name the 4 th 1-d subarray name ompilers do not alloate [i] s memory loation a 2-d array with 4 rows and 4 olumns +1 +2 +3 of Array Pointers 13

A 2-d array and its sub-arrays type sizes sizeof() = 4*4*4 bytes sizeof([i]) = 4*4 bytes sizeof([i][j]) = 4 bytes [i] [i][j] : the 2-d array name : the 1-d array name : the 0-d array name (a salar integer) a 2-d array with 4 rows and 4 olumns +1 +2 +3 of Array Pointers 14

A 2-d array and its 1-d sub-arrays a type view 2-d array name int (*) [4] 1-d array pointer int (*) [4] 1-d subarray name int [4] 1-d subarray name int [4] 1-d subarray name int [4] 1-d subarray name int [4] and different types the same address of the starting element a 2-d array with 4 rows and 4 olumns +1 +2 +3 (int (*) [4]) (int [4]) (int [4]) (int [4]) (int [4]) of Array Pointers 15

1-d subarray aggregated data type The 1 st subarray (=subarray name) sizeof() = 4*4 bytes (+0) : start address The 2 nd subarray (=subarray name) sizeof() = 4*4 bytes (+1) : start address The 3 rd subarray (=subarray name) sizeof() = 4*4 bytes (+2) : start address The 4 th subarray (=subarray name) sizeof() = 4*4 bytes (+3) : start address +0 +1 +2 +3 (int []) (int []) (int []) (int []) of Array Pointers 16

2-d array name as a pointer to a 1-d subarray 2-d array name 1-d array pointer 1-d array pointer +1 1-d array pointer +2 1-d array pointer +3 (int (*) [4]) (int (*) [4]) (int (*) [4]) (int (*) [4]) +1 +2 (int []) (int []) (int []) The 1 st subarray The 2 nd subarray The 3 rd subarray +3 (int []) The 4 th subarray of Array Pointers 17

1-d array and 0-d and 1-d array pointers 0-d array pointer : int pointer 1-d array pointer 0-d 1-d int (*m) ; int (*n) [4] ; int [4] ; int [4] ; (int (*)) m = ; (int(*)[4]) n = &; m = &; m[i] [i] (*n)[i] n[i] [i] of Array Pointers 18

2-d array and 1-d and 2-d array pointers 1-d array pointer 2-d array pointer 1-d 2-d int (*p) [4] ; int (*q) [4][4] ; int [4] [4] ; int [4][4] ; (int (*) [4]) p = ; p = &; (int(*)[4][4]) q = &; p[i] [i] (*q)[i][j] q[i][j] [i][j] of Array Pointers 19

1-d array pointer to the 1-d subarray of a 2-d array 1-d array pointer &p (int (*) [4]) p int (*p)[4] = ; p = ; An array pointer: sizeof(p) = 8 bytes 1-d sub-arrays : sizeof(*p) = 4*4 bytes q+1 (int (*) [4]) +1 +2 +3 (int []) (int []) (int []) (int []) of Array Pointers 20

2-d array pointer to a 2-d array 2-d array pointer &q (int(*)[4][4]) q int (*q)[4][4] = &; q = &; An array pointer: sizeof(q) = 8 bytes 1-d sub-arrays : sizeof(*q) = 4*4*4 bytes q+1 (int (*) [4]) +1 +2 +3 (int []) (int []) (int []) (int []) of Array Pointers 21

Using a 1-d array pointer to a 2-d array int (*p) [4] ; int [4] [4] 1-d array pointer &p (int (*) [4]) p p = ; p[i] [i] 1-d a 2-d array with 4 rows and 4 olumns (int (*) [4]) p p p p p p p p p p p p p p p p p p p p p p p p p of Array Pointers 22

Using a 2-d array pointer to a 2-d array int (*q) [4][4] ; int [4][4] ; 2-d array pointer &p (int(*)[4][4]) q q = &; (*q)[i] [i] 2-d a 2-d array with 4 rows and 4 olumns *q (int (*) [4]) C *q (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) (*q) of Array Pointers 23

(n-1)-d array pointer to a n-d array int a[4] ; 1-d array int (*p) ; 0-d array pointer (p = a) int b[4] ; 2-d array int (*q) ; 1-d array pointer (q = b) int [4] ; 3-d array int (*r) ; 2-d array pointer (r = ) int d[4] [4]; 4-d array int (*s) [4]; 3-d array pointer (s = d) of Array Pointers 24

n-d array name and (n-1)-d array pointer int a[4] ; p = &a; int (*p) ; p = a; p a a a int b[4] ; q = &b; int (*q) ; q = b; q b b b int [4] ; r = &; int (*r) ; r = ; r int d[4] [4]; s = &d; int (*s) [4]; s = d; s d d d of Array Pointers 25

n-d array pointer to a n-d array int a [4] ; 1-d array int (*p) [4]; 1-d array pointer (p = &a) int b [4]; 2-d array int (*q) [4]; 2-d array pointer (q = &b) int [4]; 3-d array int (*r) [4]; 3-d array pointer (r = &) int d [4][4]; 4-d array int (*s) [4][4]; 4-d array pointer (s = &d) of Array Pointers 26

n-d array name and n-d array pointer int a [4] ; p = &a; int (*p) [4]; &a p a a int b [4]; q = &b; int (*q) [4]; &b q b b int [4]; r = &; int (*r) [4]; & r int d [4][4]; s = &d; int (*s) [4][4]; &d s d d of Array Pointers 27

multi-dimensional array pointers p a q b r a a b a b b int a[4] ; 1-d array int (*p) ; 0-d array pointer int b[4] ; 2-d array int (*q) ; 1-d array pointer int [4] ; 3-d array int (*r) ; 2-d array pointer a b int d[4] [4]; 4-d array int (*s) [4]; 3-d array pointer of Array Pointers 28

Initializing multi-dimensional array pointers (1) p1 q2 r3 &a &b & a int a[4] ; int (*p1)[4] = &a ; b b int b[4]; int (*q2)[4] = &b; a int [4]; int (*r3)[4] = &; s4 &d d int d[4][4]; int (*s4)[4][4] = &d; d of Array Pointers 29

Initializing multi-dimensional array pointers (2) p0 q1 r2 a int a[4] ; int (*p0) = a ; b b int b[4]; int (*q1) = b; a int [4]; int (*r2) = ; s3 int d[4][4]; int (*s3)[4] = d; d d of Array Pointers 30

multi-dimensional array pointers int d[4] [4]; int (*s) [4]; d 4-d array name d[4][4] 3-d array pointer (*p)[4] d[i] 3-d array name d[i][4] 2-d array pointer (*q)[4] d[i][j] 2-d array name d[i][j][4] 1-d array pointer (*r)[4] d[i][j][k] 1-d array name d[i][j][k][4] 0-d array pointer (*s) i =[0..3], j = [0..1], k= [0..2] s d of Array Pointers 31

multi-dimensional array pointers int d[4] [4]; int (*s) [4]; d 4-d array name d[4][4] p[i][j][k][l] 3-d array pointer (*p)[4] int (*p)[4] = d; d[i] 3-d array name d[i][4] q[j][k][l] 2-d array pointer (*q)[4] int (*q)[4] = d[i]; d[i][j] 2-d array name d[i][j][4] r[k][l] 1-d array pointer (*r)[4] int (*r)[4] = d[i][j]; d[i][j][k] 1-d array name d[i][j][k][4] s[l] 0-d array pointer (*s) int (*s) = d[i][j][k]; i =[0..3], j = [0..1], k= [0..2] of Array Pointers 32

Passing multidimensional array names int a[4] ; int (*p) ; all funa(a, ); prototype void funa(int (*p), ); int b[4] ; int (*q) ; all funb(b, ); prototype void funb(int (*q), ); int [4] ; int (*r) ; all fun(, ); prototype void fun(int (*r), ); int d[4] [4]; int (*s) [4]; all fund(d, ); prototype void fund(int (*s)[4], ); of Array Pointers 33

Referenes Essential C, Nik Parlante Effiient C Programming, Mark A. Weiss C A Referene Manual, Samuel P. Harbison & Guy L. Steele Jr. [4] C Language Express, I. K. Chun