Array and structure both are the container data type. The major difference between an array and structure is that an “array” contains all the elements of “same data type” and the size of an array is defined during its declaration, which is written in number within square brackets, preceded by the array name. A “structure” contains all the elements of “different data type”, and its size is determined by the number of elements declared in a structure when it is defined.
There are some more differences between an array and structure that are explored in a comparison chart given below.
Content: Array Vs Structure
Comparison Chart
BASIS FOR COMPARISON | ARRAY | STRUCTURE |
---|---|---|
Basic | An array is a collection of variables of same data type. | A structure is a collection of variables of different data type. |
Syntax | type array_name[size]; | struct sruct_name{ type element1; type element1; . . } variable1, variable2, . .; |
Memory | Array elements are stored in contiguous memory location. | Structure elements may not be stored in a contiguous memory location. |
Access | Array elements are accessed by their index number. | Structure elements are accessed by their names. |
Operator | Array declaration and element accessing operator is "[ ]" (square bracket). | Structure element accessing operator is "." (Dot operator). |
Pointer | Array name points to the first element in that array so, array name is a pointer. | Structure name does not point to the first element in that structure so, structure name is not a pointer. |
Objects | Objects (instances) of an array can not be created. | Structure objects (instance or structure variable) can be created. |
Size | Every element in array is of same size. | Every element in a structure is of different data type. |
Bit filed | Bit filed can not be defined in an array. | Bit field can be defined in a structure. |
Keyword | There is no keyword to declare an array. | "struct" is a keyword used to declare the structure. |
User-defined | Arrays are not user-defined they are directly declared. | Structure is a user-defined datatype. |
Accessing | Accessing array element requires less time. | Accessing a structure elements require comparatively more time. |
Searching | Searching an array element takes less time. | Searching a structure element takes comparatively more time than an array element. |
Definition of Array
An array is a collection of variable or elements of same data type. The elements in an array are collectively referred by a common name which is a name of that array. The number of variables that an array will hold is defined at the time of declaration in square brackets preceded by the name of that array. In C++, there is strict bound checking on arrays; you can not store elements in an array, out of its bound.
The array can be declared as follow:
- // In C++
- type var_name[size];
- //In Java
- type var-name[ ];
- var_name = new type[size];
Here, the type describes the data type of an array and, size defines the capacity of an array. Let’s declare an array of integer type and size 10, i.e. it will hold ten elements. The indexing in arrays starts from “0” up to “size-1”.
- int p[10];
- int * ptr=p; //You can also declare a pointer to an array.
The elements in an array can be accessed in two ways, first with their “array indexing” and second with the help of “pointer arithmetic”. The efficient way to access array is “pointer arithmetic”.
- //accessing using pointer arithmetic
- Void display_array(int * S){
- while (*s){
- cout(<< "value is"<<*s);
- *s++;
- }
- }
If you want to pass an entire array to a function, then it can be passed by its name, without any index. The array passed to a function is received by its function definition, and the receiving formal parameter is always a pointer variable.
- display( p); //Call the function display.
- .
- .
- void display(int *d[]){ //Function receiving the pointer array.
- for(int i=0; i<10; i++){
- cout<< "index"<<i;
- cout<<"value at the address stored at this index"<<*p[i];
- }
- }
Definition of Structure
A structure is a collection of variables of different datatype. All the variables are the members of the structure, and all member are “public” by default. In OOP, the structures can declare both functions and variables. The structure can also be inherited. The structures are generally created to store the information.
The declaration of the structure is as follow:-
- struct sruct_name{
- type element1;
- type element1;
- .
- .
- } variable1, variable2, . .;
Let’s understand it with an example:-
- struct Student{
- string name;
- string School;
- string city;
- }stdnt1, stdnt2;
Here, we had created a structure to store the information regarding the student, and we had created instances of structure which are called variables of the structure that are stdnt1, stdnt2. These two instances share the same structure members but may contain a different value for a single member. The member of the structure variable can be accessed by using dot(.) operator for example:-
- stdnt1.name="Ajay";
- stdnt1.School="St Jhon";
- stdnt1.city="Nagpur";
The information contained in one structure variable can be assigned to the other structure variable:-
- stdnt2=stdnt1;
Member of the structure variable can be passed to a function and a whole structure and also be passed to a function and structure can also be passed using call by reference method
- funct(stdnt1.city); // passing a member of structure variable.
- funct(stdnt1); // passing whole structure to the function call by value method.
- funct(& stdnt1); //passing the structure variable using call by reference method.
We can also create a pointer to the structure:-
- srtuct Student *stdnt; //declaring the structure pointer of type 'employee'.
- stdnt=&stdnt1;
The aggregate initialization of the structure variable is also possible.
- int main(){
- struct Student={"Ajay", "St Jhon", "Nagpur"};
- }
Key Differences Between Array and Structure
- Where an Array is a collection of variables of similar data type. On the other hand, Structure is a collection of variables of dissimilar data types.
- Variable of an array are stored in a contiguous memory location whereas, the variables in a structure may or may not be stored in a contiguous memory location.
- If you want to access any variable in an array you have to access it using its index number which shows its position in that array. If you want to access a variable in a structure then you have to access it using structure name followed by a dot followed by a variable name.
- An operator used in Array is square bracket “[ ]” , it is used while Array declaration and also while accessing an array variable. An operator used in structure to access structure variable is a dot operator.
- An array name is a pointer, as the name of array points to the address of a first variable in that array. On the other hand, structure name does not point to its first element so a structure name is not a pointer.
- We can not instantiate an array whereas, we can instantiate a structure.
- All elements in an array has the same size because all elements are of the same datatype whereas, structure contain elements of dissimilar datatype hence, all elements are of different size.
- Bit-field can not be defined in an array whereas, structure allows defining bit field in it.
- Declaring array does not require any keyword. Declaring a structure requires a keyword struct.
- An array is not a user defined data type where as structure is a user-defined datatype.
- An array can be accessed faster as compared to a structure.
- Searching an element in an array is faster as compared to a structure.
Conclusion:
Arrays are accessed fast as the size of each element in an array is constant throughout. But, the structure is accessed slowly as each member in a structure variable is of different size.
No comments:
Post a Comment
commnet here