Difference between Arrays and Structures in C
Arrays | Structures |
---|---|
1. An array is a collection of related data elements of the same type. | 1. Structure can have elements of different types |
2. An array is a derived data type | 2. A structure is a programmer-defined data type |
3. Any array behaves like a built-in data types. All we have to do is to declare an array variable and use it. | 3. But in the case of structure, first, we have to design and declare a data structure before the variable of that type are declared and used. |
4. Array allocates static memory and uses index/subscript for accessing elements of the array. | 4. Structures allocate dynamic memory and uses (.) operator for accessing the member of a structure. |
5. An array is a pointer to the first element of it | 5. Structure is not a pointer |
6. Element access takes relatively less time. | 6. Property access takes relatively large time. |
Let me explain each point one by one with an example of each.
First, an array is a collection of related data elements of the same type. You can’t have an array of different data types in C (you can do this in Python). The following program throws an error in C
However, in Structure, you can have elements of different data types. The following program compiles without an error.
struct Person {
char name[10];
int age;
float height;
};
Second, an array is a derived data type. Derived data type means they are derived from fundamental data types. An array can be a collection of int, char, float, double, … but it is not a data type in itself. It adds the additional functionality to the existing data types. But, a structure is a user-defined data type. A user has all the right to create a data type to satisfy his needs. For example, the user may need a data type to represent a book. Can a book be represented by fundamental data type? Can a book be represented by an int? or Float? or String? the integer can be used represent the total number of pages, similarly, the float can be used to represent the price of the book and string can be used to represent the title and author of the book. But one integer alone can’t represent a whole book but it requires a combination of different fundamental data types.
struct Book {
char title[50];
char author[50];
float price;
int pages;
};
A structure is, therefore, a new data type. Just like int, char or float, structure creates a new data type.
This means we can have an array of structures just like we can have an array of integers. The following code snippet creates an array of structure Book.
struct Book books[2];
‘books’ is an array of data type ‘Book’ which can contain two books.
Here are other related articles related to Array
great
Is it copied?
https://in.answers.yahoo.com/question/index?qid=20121028233348AA1pOvy
Thanks
thanks
thanks
Thank that's was great.
thanks
thanks
Umm….. this was pretty useful, but I'm looking for something more specific. I would like to know the differences between an array of structures and normal structures and structures within a structure
greateeeeeeeeeeeeeeeeeeeee
greateeeeeeeeeeeeeeeeeeeee
i love this i also want to be a blogger
i lov this i want to be a pprogramer
yes
Thanks it's helpful for me keep sharing
Ty
very helpful
Thanks
no
Not satisfy
nyc its very useful.thnks
Tysm