Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched. For example, a search engine may use an array to store Web pages found in a search performed by the user. When displaying the results, the program will output one element of the array at a time. This may be done for a specified number of values or until all the values stored in the array have been output.
While the program could create a new variable for each result found, storing the results in an array is much more efficient way to manage memory. The syntax for storing and displaying the values in an array typically looks something like this:.
The above commands would print the first three values of the array, or " This is pretty simple. So not only do arrays help manage memory more efficiently, they make the programmer's job more efficient as well.
The definition of Array on this page is an original TechTerms. An array is a collection of elements of the same type placed in contiguous memory locations that can be individually referenced by using an index to a unique identifier. Five values of type int can be declared as an array without having to declare five different variables each with its own identifier.
In this case, these are values of type int. As expected, an n array must be declared prior its use. NOTE : The elements field within square brackets [], representing the number of elementsin the array, must be a constant expression, since arrays are blocks of static memory whose size must be known at compile time.
By default, are left uninitialized. This means that none of its elements are set to anyparticular value; their contents are undetermined at the point the array is declared. For example:. If declared with less, the remaining elements are set to their default values which for fundamental types, means they are filled with zeroes.
After this declaration, array foo would be five int long, since we have provided five initialization values. Therefore, there is no longer need for the equal sign between the declaration and the initializer. Both these statements are equivalent:. Static arrays, and those declared directly in a namespace outside any function , are always initialized.
If no explicit initializer is specified, all the elements are default-initialized with zeroes, for fundamental types. The values of any of the elements in an array can be accessed just like the value of a regular variable of the same type.
The syntax is:. Following the previous examples in which foo had 5 elements and each of those elements was of type int, the name which can be used to refer to each element is the following:. For example, the following statement stores the value 75 in the third element of foo :. Therefore, the expression foo[2] or foo[4] is always evaluated to an int. Notice that the third element of foo is specified foo[2] , the second one is foo[1] , since the first one is foo[0].
If we write foo[5] , we would be accessing the sixth element of foo , and therefore actually exceeding the size of the array. This can create problems, since accessing out-of-range elements do not cause errors on compilation, but can cause errors on runtime. The reason for this being allowed because index checking slows down program execution. At this point, it is important to be able to clearly distinguish between the two uses that brackets [] have related to arrays. Now if take an example of implementation of data structure Stack using array there are some obvious flaw.
The algorithm would go something like this. Check for the stack underflow Decrement the top by 1 So there what we are doing is that, the pointer to the topmost element is decremented means we are just bounding our view actually that element stays there talking up of the memory space.
If you have any primitive datatype then it might be ok but the object of an array would take a lot of memory. Applications on Array Array stores data elements of the same data type. Arrays can be used for CPU scheduling. If you like GeeksforGeeks and would like to contribute, you can also write an article using write. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article.
0コメント