20 Apr 2017

Difference between Array and Structure


Array and Structure new
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

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

BASIS FOR COMPARISONARRAYSTRUCTURE
BasicAn array is a collection of variables of same data type.A structure is a collection of variables of different data type.
Syntaxtype array_name[size];struct sruct_name{
type element1;
type element1;
.
.
} variable1, variable2, . .;
MemoryArray elements are stored in contiguous memory location.Structure elements may not be stored in a contiguous memory location.
AccessArray elements are accessed by their index number.Structure elements are accessed by their names.
OperatorArray declaration and element accessing operator is "[ ]" (square bracket).Structure element accessing operator is "." (Dot operator).
PointerArray 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.
ObjectsObjects (instances) of an array can not be created.Structure objects (instance or structure variable) can be created.
SizeEvery element in array is of same size.Every element in a structure is of different data type.
Bit filedBit filed can not be defined in an array.Bit field can be defined in a structure.
KeywordThere is no keyword to declare an array."struct" is a keyword used to declare the structure.
User-definedArrays are not user-defined they are directly declared.Structure is a user-defined datatype.
AccessingAccessing array element requires less time.Accessing a structure elements require comparatively more time.
SearchingSearching 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.
Array
The array can be declared as follow:
  1. // In C++
  2. type var_name[size];
  3. //In Java
  4. type var-name[ ];
  5. 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”.
  1. int p[10];
  2. 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”.
  1. //accessing using pointer arithmetic
  2. Void display_array(int * S){
  3. while (*s){
  4. cout(<< "value is"<<*s);
  5. *s++;
  6. }
  7. }
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.
  1. display( p); //Call the function display.
  2. .
  3. .
  4. void display(int *d[]){ //Function receiving the pointer array.
  5. for(int i=0; i<10; i++){
  6. cout<< "index"<<i;
  7. cout<<"value at the address stored at this index"<<*p[i];
  8. }
  9. }

 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:-
  1. struct sruct_name{
  2. type element1;
  3. type element1;
  4. .
  5. .
  6. } variable1, variable2, . .;
Structure
Let’s understand it with an example:-
  1. struct Student{
  2. string name;
  3. string School;
  4. string city;
  5. }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:-
  1. stdnt1.name="Ajay";
  2. stdnt1.School="St Jhon";
  3. stdnt1.city="Nagpur";
The information contained in one structure variable can be assigned to the other structure variable:-
  1. 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
  1. funct(stdnt1.city); // passing a member of structure variable.
  2. funct(stdnt1); // passing whole structure to the function call by value method.
  3. funct(& stdnt1); //passing the structure variable using call by reference method.
We can also create a pointer to the structure:-
  1. srtuct Student *stdnt; //declaring the structure pointer of type 'employee'.
  2. stdnt=&stdnt1;
The aggregate initialization of the structure variable is also possible.
  1. int main(){
  2. struct Student={"Ajay", "St Jhon", "Nagpur"};
  3. }

 Key Differences Between Array and Structure

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. We can not instantiate an array whereas, we can instantiate a structure.
  7. 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.
  8. Bit-field can not be defined in an array whereas,  structure allows defining bit field in it.
  9. Declaring array does not require any keyword. Declaring a structure requires a keyword struct.
  10. An array is not a user defined data type where as structure is a user-defined datatype.
  11. An array can be accessed faster as compared to a structure.
  12. 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