1 / 17

Chapter.8 Arrays and Pointers

Chapter.8 Arrays and Pointers. 8.1 Need for arrays and pointers Arrays are important to engineers because so many problems involve parameters that can be treated as arrays-for example, vectors, matrices…etc.

zenia
Télécharger la présentation

Chapter.8 Arrays and Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter.8 Arrays and Pointers • 8.1 Need for arrays and pointers • Arrays are important to engineers because so many problems involve parameters that can be treated as arrays-for example, vectors, matrices…etc. • The power of pointers will become apparent, especially as we move onto dynamic memory allocation.

  2. 8.2 Introduction to arrays Arrays are useful whenever a group of tightly related variables is required.

  3. 8.2.1 Definition of Arrays type taVar [ constant size ] ; int iaArray [ 4 ] ; #define ARRAYSIZE 4 int iaArray [ ARRAYSIZE ];

  4. 8.2.2 Array Indexing int iIndex; int iaArray [4]; for ( iIndex = 0 ; iIndex < 4 ; ++iIndex ) iaArray [iIndex]=0;

  5. 8.2.3 Array Bounds The bottom line is – do not access memory out side of the allocated array bounds.

  6. 8.3 Initializing the contents of an array After an arrays is specified (or allocated), the array will have random values until its contents are initialized.

  7. 8.3.1 Initialization list int iaVol [10]={0,1,2,3,4,5,6,7,8,9}; int iaVol [ ]={2,3,4}; #define MONTHS 12 … const int kiaDaysInMonth [ MONTHS ]={31,28,31, 30,31,30, 31,31,30, 31,30,31 };

  8. enum { Months = 12 }; const int kiaDaysInMonth [ MONTHS ]={31,28,31, 30,31,30, 31,31,30, 31,30,31 };

  9. 8.3.2 Explicit Array Initialization Explicit array initialization occurs when the arrays values are calculated in the program and assigned to the array, following the array definition. #define WeekDays 7; float faweeklyRain[ WeekDays ]; int iDay; for (iDay=0;iDay<WeekDays;++iDay) faWeeklyRain[iDay]=0.0;

  10. 8.3.3 Static Allocation Variables and arrays are automatic by default #define WeekDays 7; static float faWeeklyRain [WeekDays]; all the array numbers are initialized to zero.

  11. 8.4 Character Arrays The importance of character strings stems from their wide use in computing. By convention, a character string is terminated by the null character ’ \0 ’ .

  12. 8.4.1 Initial Character Arrays #define myCharacterArray = 6; char caMystring [myCharacterArray] ; caMystring[0]=‘o’; caMystring[1]=‘z’; caMystring[2]=‘o’; caMystring[3]=‘n’; caMystring[4]=‘e’; caMystring[5]=‘0’;

  13. Character strings may also be stored as a finite sequence of characters enclosed in double quotes. “ ozone ” “ this is a longer string” “ x=y” char caMystring[]=“a test string”

  14. 8.6 Multidimensional Arrays v1=110 * i1 – 10 * i2 v2= 10 * i1 – 15 * i2

  15. #define Rows 2; #define Cols 2; float faaResVals [Rows][Cols]; faaResVals [0][0]=110; faaResVals [0][1]=-10; faaResVals [1][0]=10; faaResVals [1][1]=-15;

  16. 8.6.1 Pattern of Storage for Multidimensional Arrays float faaArray[3][4];

  17. 8.6.2 Initializing Multidimensional Arrays #define Rows 2; #define Cols 2; Float faaResVals[Rows][Cols]={{110,-10},{10,-15}}; #define Cols; Float faaResVals[][Cols]={{110,-10},{10,-15}};

More Related