C++ For Science and Engineering Lecture 16

C++ For Science and Engineering Lecture 16 John Chrispell Tulane University Friday October 1, 2010 arrfun1.cpp Notes on the previous listing: The n...
Author: Maud Fields
1 downloads 2 Views 178KB Size
C++ For Science and Engineering Lecture 16 John Chrispell Tulane University

Friday October 1, 2010

arrfun1.cpp Notes on the previous listing: The name of an array is the same as if it were a pointer. c o o k i e s == &c o o k i e s [ 0 ] // a r r a y name i s a d d r e s s o f f i r s t e l e m e n t .

There are two exceptions to this rule. 1 2

Array declarations use the array name to lable the storage. Applying sizeof to an array name yields the size of the whole array in bytes.

The notations int ∗ arr and int arr [ ] // a r e o n l y synonymous i n t h e f u n c t i o n h e a d e r and p r o t o t y p e .

we can not use int arr[] to declare a pointer in the body of a function. RECALL a r r [ i ] == ∗ ( a r r + i ) // v a l u e s i n two n o t a t i o n s &a r r [ i ] == a r r + i // a d d r e s s e s i n two n o t a t i o n s

Consider the following example: John Chrispell,

Friday October 1, 2010

slide 3/25

arrfun2.cpp #i n c l u d e const i n t A r S i z e = 8 ; int sum arr ( int a rr [ ] , int n ) ; // u s e s t d : : i n s t e a d o f u s i n g d i r e c t i v e i n t main ( ) { int cookies [ ArSize ] = {1 ,2 ,4 ,8 ,16 ,32 ,64 ,128}; s t d : : c o u t

Suggest Documents