30 likes | 49 Views
C is a programming language .C basically used in most of the other programming languages also a Mysql has been written with help of c and c fundamentals and Array are particularly used in C language .array is a compound data type that its store multiple values of same data types like int,double ,float etc. We use Pointer Arithematic to find the Length of Array in C.<br>
E N D
How to Find the Length of an Array in C? C is a programming language that has been known for its simplicity and the ease of compiling any program. Dennis Ritchie is the known founder of the C language and has been involved in various other programming language’s development. C is the base of various languages for instance, MySQL has been written with the help of C and C++ fundamentals. Array is a vital part of the C language that is basically one of its components. An array in basically a collection of items that are stored at a contiguous memory locations in C and C++, and the elements can generally be accessed randomly just by the use indices of an array. Arrays are the components that can be used basically to store a collection of primitive data types like int, double, float, char, etc that are of any particular type. To find the length of an array we should follow the generic approach that is given below: Arrays are basically known to have a fixed lengths that is generally within the scope of their declarations. Apart from this fact, users can obviously find the length of an array. Whenever the array’s length is determined automatically by an initializer then this has been always known to make the code very flexible. Consider this code for reference:
int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; /* size of `array` in bytes */ size_t size = sizeof(array); /* number of elements in `array` */ size_t length = sizeof(array) / sizeof(array[0]); Whenever it is found that an array is appearing in an expression then it is automatically converted to or can be said that it decays to a pointer to its first element and it can be found generally in most of the contexts. In the case where an array is found to be the operand of the sizeof operator in C and is generally one of a small number of exceptions then the resulting pointer does not carry any information about the length of the array from which it has been derived and ultimately it is not itself an array. Hence, if there is any need to find the length of an array in conjunction with the pointer like when the pointer is passed to a particular function then this information must be conveyed separately. For instance, let’s assume that the user want to write a function in order to return the last element of an array of int. As from the above code you can say that: /* array will generally decay to a pointer hence, there is a need to pass the length separately */ int last = get_last(array, length);
And the function should be implemented like as depicted below: int get_last(int input[], size_t length) { return input[length - 1]; } Please note that the declaration of the parameter input generally resembles that of an array in C and as of the fact the array declares input as a pointer (to int). It can be said that this is exactly equivalent to declaring input as int *input. And the exact could be true even regardless of the fact that dimensions are available. This is generally made possible as of the fact that the arrays cannot ever be actual arguments to the functions and this can be viewed as mnemonic. You can generally find the length of an array by the above mentioned technique and to learn more about these topics, you can log on to phptpoint.com and learn our C tutorial that is the finest C tutorial you will ever find with the exact index and elaborative examples. Our C tutorial for beginners basically contains every basic things along with the caution points that will help the beginners to grow in the field. Learn Ajax in Simple Way - Ajax Programming Tutorial Original Source