Sei sulla pagina 1di 2

ARRAYS----> From bookish point of view...

an array is a collection of homogeneous data, by ho mogeneous we mean dat the data vil be of same data type,may it be int or char or float. int arr[10]..is an array of data type integer with 10 variables occupying size o f 40 bytes. Dis vil be same as defining int a0,a1,a.....a9 But the difference is dat v can now access dem easily through 1 name. for eg: if v want to access 2nd element we will do like arr[1] ,5th element arr[ 4] and so on. Remember: array-name[index] array-name can be anythng and index starts from 0 and not 1. an array of 5 elements: int arr[5] defining simple variables : arr1 arr2 arr3 arr4 arr5 using array arr[0] arr[1] arr[2] arr[3] arr[4] notice the indices Hope u get it..!! You can treat them just like normal variables but accessing them through array s tyle. Remember: Arrays are allocated in memory contiguously. for eg: in ram arr[0],arr[1],arr[2] will be adjuacent to each other like address of arr[0] is 1000,address[1] is 1004 and so on...!! Now internally arrays are nothing more than "CONSTANT POINTERS". For eg: u declare int arr[10]; then arr is only the address of arr[0] and it vil always point to that location only For eg : int arr[5]; lets assume arr is pointing to 1000 So what happens when u require arr[3]. arr + index*sizeof(datatype) this formula is used 1000 + 3*4 //sizeof(int) is 4 which gives 1012 this vil be the address of arr[3]. Remember when i told u that arrays are allocated contigously in memory.This is t he main importance . if they are not allocated contiguously then this scheme wou ld fail. Since the compiler just multiplies d index with the size of data type and adds i t to base address...u vil be surprised to knw that u can also use array like 3[arr]..this vil be same as arr[3]. and remember that arrays are like constant pointers internally you cannot assign an array to another variable once its been defined. and due to the way that arrays are implemented internally they hav O(c) access t ime.. which means constant access time. for further experience actually code the arrays for first hand experience.

Few things about MAIN() We all are awrae of tis function,right? We all love this function coz its the en try point of our software.Actually before main() is entered preprocessor is run which processes everything defined/declared before entering main(),for eg: inclu ding header files,processing macros, etc. By ISO standard main() should always return int. You should always declare int main(), you can also use void but remember the sta ndard is using "int main()". It should return 0 on successful completion and 1 on unsuccessful completion. Remember you can also return any other value like return 10, return 23 anything

but remeber what the standard is. Passing Arguments to main()----> int main( int argc, char* argv[] ) this is the syntax for main(),expecting 2 arguments ARGC( ARGUMENT COUNT ) of ty pe int and ARGV( ARGUMENT VECTOR ) of type char**.It can also be denoted as char *[].It means that it points to an array of char*.You can also declare like int main( int argc, char** argv ) There are at least two arguments to main: argc and argv. The first of these is a count of the arguments supplied to the program and the second is an array of po inters to the strings which are those arguments its type is (almost) array of point er to char . Now u run the programme and say "hey, Wait id dint paased any arguments" REMEMBER: that u pass arguments through command line i.e dos in other wrds comma nd prompt(CMD).For any1 who hasnt seen cmd type cmd in run(win + r). When a program starts, the arguments to main will have been initialized to meet the following conditions: 1. argc is greater than zero. 2. argv[argc] is a null pointer. 3. argv[0] will be a string containing the program's name or a null string if th at is not available. Remaining elements of argv represent the arguments supplied to the program. In cases where there is only support for single-case characters , the contents of these strings will be supplied to the program in lower-case. Now wat u actually do with these arguments inside your programme depends on you. The most imp thing to remember is that : ARGC(ARGUMENT COUNT) tells us how many arguments are passed to your programme wh ich are stored in ARGV( ARGUMENT VECTOR ). Default value of ARGC is 1 because the first argument inside ARGV is the path of the exe file itself(m talkng abt windows, dnt knw bt linux :( ). A sample programme: #include <stdio.h> int main(int argc, char **argv) { while(argc--) printf("%s\n", *argv++); return 0; } But on visual studio 2005 the implementation is a little diff. for eg: if you pass 2 arguments to the programme,then argv[2] will be the name o f the programme and not argv[0]. rest is same.

Potrebbero piacerti anche