Arrays in C

文章推薦指數: 80 %
投票人數:10人

The Method 1 Way (single Malloc) and function parameters. The base address of an array allocated this way is a pointer to an int, so it can be passed to a ... ArraysinC Chassupportforsingleandmultidimensionalarrays. Arrayscanbestaticallyallocatedordynamicallyallocated. Thewayinwhichyouaccessthearrayanditstypevariesbasedon howitisdeclaredandallocated. InformationaboutStaticallyAllocatedArrays InformationaboutDynamicallyAllocatedArrays InformationaboutDynamicallyAllocated2DArrays staticallydeclaredarrays Thesearearrayswhosenumberofdimensionsandtheirsizeareknownat compiletime. Arraybucketvaluesarestoredincontiguous memorylocations(thuspointerarithmeticcanbeusedtoiterateover thebucketvalues),and2Darraysareallocatedinrow-majororder (i.e.thememorylayoutisallthevaluesinrow0first,followedby thevaluesinrow1,followedbyvaluesinrow2...). Staticallydeclaredarrayscanbedeclaredaseither globalorlocalvariables. 1-Darrays Someexamplesofdeclarationanuse: inta1[100];//declareastaticarray,a1,of100ints charc1[50];//declareastaticarray,c1,of50chars //accessingarrayelementsusingindexing for(i=0;i<100;i++){ a1[i]=0; } ArrayParameters Thebaseaddressofanarrayispassedtoafunctiontakingan arrayparameter(thismeansthattheparameterpointstothesame arraybucketsasitargument,andthusthroughtheparameterthe functioncanmodifythebucketvalues): //passinthedimentionofthearray,n,tomakethis //functionworkforanysizearray voidinit_array(intarr[],intn){ inti; for(i=0;i[0,0,...,0,0,...0,0,...] //accessusing[]notation: //cannotuse[i][j]syntaxbecausethecompilerhasnoideawherethe //nextrowstartswithinthischunkofheapspace,somustusesingle //indexvaluethatiscalculatedusingrowandcolumnindexvaluesand //thecolumndimension for(i=0;i|*-|------->[0,0,0,...,0]row0 //|*-|------->[0,0,0,...,0]row1 //|...|... //|*-|------->[0,0,0,...,0] //accessusing[]notation: //2d_array[i]isithbucketin2d_array,whichistheaddressof //a1darray,onwhichyoucanuseindexingtoaccessitsbucketvalue for(i=0;i



請為這篇文章評分?