Array Representation in Data Structures
Last Updated :
20 Jul, 2023
Representation of Array
The representation of an array can be defined by its declaration. A declaration means allocating memory for an array of a given size.
Array
Arrays can be declared in various ways in different languages. For better illustration, below are some language-specific array declarations.
C++
int arr[5];
char arr[10];
float arr[20];
|
C
int arr[5];
char arr[10];
float arr[20];
|
Java
<data type><variable name>[]
= {<data1>, <data2>,…..<dataN> };
int arr[] = { 2 , 5 , 6 , 9 , 7 , 4 , 3 };
|
Python
arr = [ 10 , 20 , 30 ]
arr2 = [ 'c' , 'd' , 'e' ]
arr3 = [ 28.5 , 36.5 , 40.2 ]
|
C#
Javascript
let arr=[10,20,30];
let arr2 = [ 'c' , 'd' , 'e' ]
let arr3 = [28.5, 36.5, 40.2]
|
Array declaration
However, the above declaration is static or compile-time memory allocation, which means that the array element’s memory is allocated when a program is compiled.
Here only a fixed size (i,e. the size that is mentioned in square brackets []) of memory will be allocated for storage, but don’t you think it will not be the same situation as we know the size of the array every time, there might be a case where we don’t know the size of the array. If we declare a larger size and store a lesser number of elements will result in a waste of memory or either be a case where we declare a lesser size then we won’t get enough memory to store the rest of the elements. In such cases, static memory allocation is not preferred.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...