Computer Science, asked by Anusha4466, 1 year ago

How is Structure different from an Array?

Answers

Answered by prateekgoyal497
0

Arrays are a homogenous linear data structure which stores data in a contiguous fashion.

Let’s break down the definition to more simpler terms:

Homogenous: Takes only single type of inputs, say

int arr[3] = {3,7,8};  //A typical integer array in C

Linear: The elements are stored in a sequence or block and thus can be accessed with the help of indices and iterators.

cout<< arr[2]; //This will print 8

Contiguous: Since the data structure is linear in nature , the memory allocation for the element is also sequential and can be derived by knowing the address for the first element, sometimes also referred as the base address

mem_addr(arr[i]) = mem_addr(arr[i-1]) + sizeof(data_type)

mem_addr(arr[0]) = 1004  //Say the first element is at the location 1004

Then,

mem_addr(arr[1]) = 1004 + 4 //1008

mem_addr(arr[2]) = 1008 + 4 //1012

Structures on the other hand are not homogenous. Unlike arrays , they can accept different kinds of input irrespective of the data type.

struct records{  //The structure tag verifies structure name

int marks[100];

char names[50];

int age[100];

char grade[100];

};

Moreover, a structure is user-defined data structure i.e. the user has to supply a design of the data structure before declaring a structure variable.

The memory allocation in case of structures is dynamic and compaction of data members is possible through the use of bit fields.

Structures make use of the (.) dot operator to access data members.

Answered by Anonymous
1

structusre 4is a collection of variable under one name.

an array is a collection of variables of the same type that are referenced by a common name.

hope rt will help u

mark as brainliest

Similar questions