Pointers, Arrays, Multidimensional cse251/  · Pointers, Arrays, Multidimensional Arrays • Pointers

  • View
    213

  • Download
    0

Embed Size (px)

Text of Pointers, Arrays, Multidimensional cse251/  · Pointers, Arrays, Multidimensional Arrays •...

Pointers,Arrays,MultidimensionalArrays

Pointersversusarrays Lotsofsimilarities

Howtodealwith2D,3D,multidimensionalarrays(for storing matrices and other 2D or 3D data!)(forstoringmatricesandother2Dor3Ddata!)

CSE251Dr.CharlesB.OwenProgramminginC1

Review:PointersAddress Memory Name

Pointersarevariablesthatstorememoryaddresses

Address Memory

0xeffffa94

Name

a15

int a = 15;

0xeffffa98 b0xeffffa94

int a 15;int *b=&a;

printf(%x%x%d\n,b,&b,*b);// prints effffa94 effffa98 15//printseffffa94effffa9815

CSE251Dr.CharlesB.OwenProgramminginC2

int number;int *ptr =&number;

Usingpointersinscanf function

Read&as at

printf(Enteraninteger:);scanf(%d &number);

scanf functionas at

scanf( %d , &number);printf(Enteranotherinteger:);scanf(%d, ptr);

Donthavetoput&beforeptr.Itsalready an at

printf(Number=%d,*ptr =%d\n,number,*ptr);

alreadyan at .

Exampleoutput:

CSE251Dr.CharlesB.OwenProgramminginC3

int multiply(int *,int);

int main()

Passingpointers(addresses) toint main()

{int number=3;int *ptr &n mber;

(addresses)tofunctions

int *ptr =&number;printf(1:%d\n,multiply(&number,2));printf(2:%d\n,multiply(ptr,3));

}}

int multiply(int *a, int factor){{

return(*a) *factor;}

CSE251Dr.CharlesB.OwenProgramminginC4

Review:Arrays

Anarrayisacontiguouschunkofmemorytostoremultiplevalues

int grades[]={74,59,95,85,71,45,99,82,76};

0xeffffa00 0xeffffa04 0xeffffa08 0xeffffa0c 0xeffffa10 0xeffffa14 0xeffffa18 0xeffffa1c 0xeffffa20

grades 74 59 95 85 71 45 99 82 76

index 0 1 2 3 4 5 6 7 8

CSE251Dr.CharlesB.OwenProgramminginC5 A

int sumArray(int [],int);

int main()Passingarraystofunctions{

int list[]={1,2,3,4};printf(Sum=%d\n,sumArray(list ,4));

functions

}

int sumArray (int list[],int arraySize){{

int sumvalue =0;for(int i=0;i

ArrayNameffe2de0c ffe2de10 ffe2de14 ffe2de18Thearraynameisa

pointertothefirstl t f th

1 2 3 4

[1] [2] [3][0]

listffe2de0c ffe2de10 ffe2de14 ffe2de18

elementofthearray [1] [2] [3][0]

int list[]={1,2,3,4};

Output: ffe2de0c ffe2de0c 1

int list[] {1,2,3,4};printf(%x,%x,%d,list,&list[0],*list);

Output:ffe2de0cffe2de0c 1

CSE251Dr.CharlesB.OwenProgramminginC7

PointersandArrays p

int *p,int list[] {1 2 3 4};

1 2 3 4

[1] [2] [3][0]

list

int list[]={1,2,3,4};p=list; /*equivalenttop=&list[0]*/printf(%d\n,*p);/*printsthevalue1*/

[ ] [ ] [ ][ ]

YYoucanuseapointertoaccess

thearray

CSE251Dr.CharlesB.OwenProgramminginC8

Pointerand[] P

Anypointertoablockofmemorycanusethe[]syntax,evenifitis

not declared as an array! 1 2 3 4Listnotdeclaredasanarray!

[1] [2] [3][0]

int *p,pint list[]={1,2,3,4};p=list;i tf(%d\ [2]) // i t 3

int *v;andint v[];/*Meanthesamething*/

printf(%d\n,p[2]); //prints3

CSE251Dr.CharlesB.OwenProgramminginC9

Arrayindexing[] *list Contentspointedtobylist*(list+2) Contentsatlist[2]

Indexinganarrayisjustaway

list

ff 2d 0 ff 2d 10 ff 2d 14 ff 2d 18

list+2

offindingaparticularaddressinthatblock1 2 3 4

[1] [2] [3][0]

ffe2de0c ffe2de10 ffe2de14 ffe2de18

[1] [2] [3][0]

int list[] = {1 2 3 4} // array of 4 intsint list[]={1,2,3,4} //arrayof4intsprintf(%d,list[2]);

Thisisequivalenttoprintf(%d,*(list+2));printf( %d , (list+2));

CSE251Dr.CharlesB.OwenProgramminginC10 B

PointerArithmetic

Whenweaddtoapointer,suchas(p+1),wedontliterallyadd1tothepointeraddress

Insteadweaddoneaddresstothepointer

CSE251Dr.CharlesB.OwenProgramminginC11

PointerArithmeticint list[]={1,2,3,4};int *p=list; /*sameasp=&list[0]*/printf(%x,p); /* prints ffe2de0c */

p

printf( %x ,p);/ printsffe2de0c /

ffe2de0c ffe2de10 ffe2de14 ffe2de18

1 2 3 4

CSE251Dr.CharlesB.OwenProgramminginC12

PointerArithmeticint list[]={1,2,3,4};int *p=list; /*sameasp=&list[0]*/printf(%x,p); /* prints ffe2de0c */printf( %x ,p);/ printsffe2de0c /p=p+1; /*pincreasesby4*/printf(%x,p);/*printsffe2de10*/

pThinkofpointerarithmeticasadd

1 2 3 4

ffe2de0c ffe2de10 ffe2de14 ffe2de181locationinsteadofonebyte or addressbyteoraddress.

CSE251Dr.CharlesB.OwenProgramminginC13

PointerArithmeticd bl li t2[] {1 0 2 0 3 0}doublelist2[]={1.0,2.0,3.0};double*p=list2;/*sameasp=&list2[0]*/printf(%x,p);/*printsffe2de0c*/

p

p ( p) p

1.0

ffe2de0c ffe2de10 ffe2de14 ffe2de18

2.0 3.0

ffe2de1c ffe2de20

1.0 2.0 3.0

CSE251Dr.CharlesB.OwenProgramminginC14 C

PointerArithmeticd bl li t2[] {1 0 2 0 3 0}doublelist2[]={1.0,2.0,3.0};double*p=list2;/*sameasp=&list2[0]*/printf(%x,p);/*printsffe2de0c*/p ( p) pp=p+1;/*Pincreasesby8bytes*/printf(%x,p);/*printsffe2de14*/

P

1.0

ffe2de0c ffe2de10 ffe2de14 ffe2de18

2.0 3.0

ffe2de1c ffe2de20

CSE251Dr.CharlesB.OwenProgramminginC15

PointerArithmeticonArrays*(l ) f h l h *(list+1)referencesthenextelementinthearray(equivalenttolist[1])

Becareful:*(++list)workstoobutnowwehavelostour pointer to the beginning of the array!!!ourpointertothebeginningofthearray!!! Equivalentto: list=list+1;*list;

CSE251Dr.CharlesB.OwenProgramminginC16

sizeof()operatori t i

Returnsthenumberof bytes needed toint i;

int *ptr4i=&i;int IntArray[]={1,2,3,4,5};double j;

ofbytesneededtostoreavariableoradata typedoublej;

double*ptr4j=&j;doubledoubleArray[]={1.0,2.0,3.0,4.0,5.0};

i tf("Si f i t i %d b t \ " i f(i t))

datatype

printf("Sizeof integeris%dbytes\n",sizeof(int));printf("Sizeof doubleis%dbytes\n",sizeof(double));

printf("Sizeof i is%dbytes\n",sizeof(i));printf("Sizeof pointerfori is%dbytes\n",sizeof(ptr4i));

printf("Sizeof jis%dbytes\n",sizeof(j));printf("Sizeof pointerforjis%dbytes\n",sizeof(ptr4j));

printf("Sizeof intArray is%dbytes\n",sizeof(intArray));printf("Sizeof doubleArray is%dbytes\n",sizeof(doubleArray));

CSE251Dr.CharlesB.OwenProgramminginC17

sizeof()operator

CSE251Dr.CharlesB.OwenProgramminginC18

WhenwepassanarrayWh i thWhenwepassanarray,wearepassingthearrayaddress

int sumArray( int [ ] int);int sumArray(int [],int);

int main(){{int list[]={1,2,3,4);

A ( li 4 )sumArray(list ,4);

CSE251Dr.CharlesB.OwenProgramminginC19

Whenwepassanarray

Thiswillworktoo(becausearraynameisapointertothebeginningofthearray)

int sumArray(int *, int);int main(){{int list[]={1,2,3,4);sumArray(list ,4);

CSE251Dr.CharlesB.OwenProgramminginC20

Whenwepassanarray

ButthisDOESNOT work!int sumArray(int *,int);y( , );int main(){int *list {1 2 3 4);int *list={1,2,3,4);sumArray(list,4);

CSE251Dr.CharlesB.OwenProgramminginC21

Pointers *list Contentspointedtobylist*(list+2) Contentsatlist[2]

Indexinganarrayisjusta

list

ff 2d 0 ff 2d 10 ff 2d 14 ff 2d 18

list+2

wayoffindingaparticularaddressinthatblock1 2 3 4

[1] [2] [3][0]

ffe2de0c ffe2de10 ffe2de14 ffe2de18

[1] [2] [3][0]

int list[] = {1 2 3 4} // array of 4 intsint list[]={1,2,3,4} //arrayof4intsprintf(%d,list[2]);

Thisisequivalenttoprintf(%d,*(list+2));printf( %d , (list+2));

CSE251Dr.CharlesB.OwenProgramminginC22 1

2DArrays

int cave[ArraySize][ArraySize];

Column

1 2 3 400 1 2 3

Column

1 2 3 45 6 7 89 10 11 12

0

1

2Row

9 10 11 1213 14 15 16

2

3

CSE251Dr.CharlesB.OwenProgramminginC23

2DArraysint myMatrix[3][4] = { {1 2 3 4} {5 6 7 8} {9 10 11 12} };int myMatrix[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

0 1 2 3

Column

1 2 3 45 6 7 8

myMatrix[0][1] 2

0 1 2 3

0

1Row 5 6 7 89 10 11 12

M t i [2][3] 12

2

Row

myMatrix[2][3] 12myMatrix[row][col]

CSE251Dr.CharlesB.OwenProgramminginC24

Physically,inoneblockofmemoryint myMatrix[2][4]={{1,2,3,4},{5,6,7,8}};

ffe2de0c ffe2de10 ffe2de14 ffe2de18 ffe2de1c ffe2de20 ffe2de24 ffe2de28

1 2 3 4 5 6 7 8ffe2de0c ffe2de10 ffe2de14 ffe2de18 ffe2de1c ffe2de20 ffe2de24 ffe2de28

Array elements are stored in row major order

row1 row2

ArrayelementsarestoredinrowmajororderRow1first,followedbyrow2,row3,andsoon

CSE251Dr.CharlesB.OwenProgramminginC25

2DArrayNameandAddresses[ ][ ] { { } { } }

ffe2de0c ffe2de10 ffe2de14 ffe2de18 ffe2de1c ffe2de20 ffe2de24 ffe2de28

int myMatrix[2][4]={{1,2,3,4},{5,6,7,8}};

1 2 3 4 5 6 7 8ffe2de0c ffe2de10 ffe2de14 ffe2de18 ffe2de1c ffe2de20 ffe2de24 ffe2de28

myMatrix:pointertothefirstelementofthe2DarraymyMatrix[0]:pointertothefirstrowofthe2DarraymyMatrix[1]:pointertothesecondrowofthe2Darray*myMatrix[1]istheaddressofelementmyMatrix[1][0]

CSE251Dr.CharlesB.OwenProgramminginC26

Accessing2DArrayElements[ ][ ] { { } { } }int myMatrix[2][4]={{1,2,3,4},{5,6,7,8}};

1 2 3 4 5 6 7 81 2 3 4 5 6 7 8

Indexing:myMatrix[i][j]issameasg y [ ][j]*(myMatrix[i]+j)(*(myMatrix +i))[j]*((*(myMatrix + i)) + j)*((*(myMatrix +i))+j)*(&myMatrix[0][0]+4*i +j)

CSE251Dr.CharlesB.OwenProgramminginC27

Declaration#defineROWS3#defineCOLS5

int table[ROWS][COLS];

voiddisplay(