Sunteți pe pagina 1din 4

Arrays Array by definition is a variable that hold multiple elements which has the same data type.

Array is variables that hold multiple elements which has same data type. An array is a multi element box, uses an indexing system.

Declaring the array: We can declare an array by specify its data type, name and the number of elements the array holds between square brackets immediately following the array name. Name; Type of array Number of element "data type array name[size];" There are some rules for array declaration. The data type can be any valid C data types including structure and union. The array name has to follow the rule of variable and the size of array has to be a positive constant integer. We can access array elements via indexes array_name[index]. Indexes of array starts from 0 not 1 so the highest elements of an array is array_name[size-1]. Initializing the array: It is like a variable, an array can be initialized. To initialize an array, you provide initializing values which are enclosed within curly braces in the declaration and placed following an equals sign after the array name. int list[5]={2,3,4,5,6} OR int list[5] = {2,1,3,7,8}; Character arrays: char string1[]=first;, "Here ,we using char string and its array size is six." char string[]={f,g,t,y,u,I,\0}; // \0 null character terminating the string. Passing the array To pass an array argument in a function specifies the name of array to pass without any bracket. int myarray[24]; myfunction(my array,24). array usually pass to the function array using call by reference function knows where the array stored passing array element: using call by value pass subscripted name (ex: myarray[5]) to function. Function prototype: void modify array (int b[],int array size); Parameter name may be optional. int b[] may be int[] int array size may be int Multidimensional Array: An array with more than one index value is called a multidimensional array. All the array above is called single-dimensional array. To declare a multidimensional array you can do

follow syntax data_type array_name[][][]; The number of square brackets specifies the dimension of the array. For example to declare two dimensions integer array we can do as follows: 1. int matrix[3][3]; Initializing Multidimensional Arrays You can initialize an array as a single-dimension array. Here is an example of initialize an two dimensions integer array: 1. 2. 3. 4. 5. 6. int matrix[3][3] = { {11,12,13}, {21,22,23}, {32,31,33}, };

Two dimensional arrays

Multidimensional array have two or more than indexes values which specify the element in the array. i.e., multi[i][j]. Where [i] specify row and [j] specify column Declaration and calculation: int a[10][10]; int b[2][2]; // int b [2][2]={{0,1},{2,3}} Sum = a[10][10]+b[2][2] Array and Pointer

Each array element occupies consecutive memory locations and array name is a pointer that points to the first element. Beside accessing array via index we can use pointer to manipulate array. This program helps you visualize the memory address each array elements and how to access array element using pointer. #include <stdio.h> void main() { const int size = 5; int list[size] = {2,1,3,7,8}; int* plist = list; // print memory address of array elements for(int i = 0; i < size;i++) { printf("list[%d] is in %d\n",i,&list[i]); } // accessing array elements using pointer for(i = 0; i < size;i++) {

printf("list[%d] = %d\n",i,*plist); /* increase memory address of pointer so it go to the next element of the array */ plist++; .}} Here is the output list[0] is in 1310568 list[1] is in 1310572 list[2] is in 1310576 list[3] is in 1310580 list[4] is in 1310584 list[0] = 2 list[1] = 1 list[2] = 3 list[3] = 7 list[4] = 8 You can store pointers in an array and in this case we have an array of pointers. " int *ap[10];" What is function? A fuction is a self contained block of statement that perform a coherent taskof some kind. main() { message(); printf(hi) } message() { printf(smile ) } Write Any C program using at least one function If a program contain only one function , it must be main() If a program contain more than one function then these can be contain in main(). There is no limit on number that might be present in a C program Each function in a program is called sequence specified by the function calls in main(). A function can be called from other function but can not be defined in the other function. There are two types of function : a) library function(printf() ,scanf()). b) User defined (my() ). Passing the values between the function : main() { int a ,b,c,sum; printf(enter the values ); scanlf(%d%d%d,%a,%b,%c);

sum=calsum(a,b,c); // actual argu./calling function printf(\nsum=%d,sum); } Callsum(x,y,z) // called function int x,y,z; { Int d; D=x+y+z; return(d); } // a function can be return only one value at a time WHAT IS A REGISTER VARIABLE? A computer can keep data in a register or in memory. A register is much faster in operation than memory but there are very few registers available for the programmer to use. variables are to be stored in a register in order to speed up the execution of the program. compiler probably allows you to use one or more register variables and will ignore additional requests if you request more than are available. WHAT IS PROTOTYPING? A prototype is a model of a real thing and when programming in ANSI-C,ability to define a model of each function for the compiler. The compiler can then use the model to check each of your calls to the function and determine if you have used the correct number of arguments in the function call and if they are of the correct type. By using prototypes, the compiler do some additional error checking. The ANSI standard for C contains prototyping as part of its recommended standard. Every ANSI-C compiler will have prototyping available. What is pre-processor? The pre-processor is executed just prior to the execution of the compiler. It's operation is transparent to you but it does a very important job. It removes all comments from the source and performs a lot of textual substitution based on your code, passing the result to the compiler for the actual compilation of your code. How to use the function Before using a function we should declare the function so the compiler has information of function to check and use it correctly. The function implementation has to match the function declaration for all part return data type, function name and parameter list. When you pass the parameter to function, they have to match data type, the order of parameter.

S-ar putea să vă placă și