Difference between Arrays and Structures in C
Arrays Structures
1. An array is a collection of related 1. Structure can have elements of
data elements of same type. different types.
2. An array is a derived data type. 2. A structure is a programmer-defined
data type.
3.Array allocates static memory and 3.Structures allocate dynamic memory
uses index / subscript for accessing and uses (.) operator for accessing the
elements of the array. member of a structure.
4.Array is a pointer to the first 4.Structure is not a pointer.
element of it.
5.Array element access takes less 5.Structures element access takes more
time in comparison with time in comparison with Array.
structures.
6. Any array behaves like a built-in 6. But in the case of structure, first we
data types. All we have to do is to have to design and declare a data
declare an array variable and use it. structure before the variable of that type
are declared and used.
Nested Structure
Nested structure in C is nothing but structure within structure. One structure can be declared
inside other structure as we declare structure members inside a structure. The structure variables
can be a normal structure variable or a pointer variable to access the data. You can learn below
concepts in this section.
1. Structure within structure in C using normal variable
2. Structure within structure in C using pointer variable
3. Structure written inside another structure is called as nesting of two structures.
4. Nested Structures are allowed in C Programming Language.
5. We can write one Structure inside another structure as member of another structure.
struct date
{
int date;
int month;
int year;
};
struct Employee
{
char ename[20];
int ssn;
float salary;
struct date doj;
}emp1;
#include <stdio.h>
struct Employee
{
char ename[20];
int ssn;
float salary;
struct date
{
int date;
int month;
int year;
}doj;
}emp = {"Pritesh",1000,1000.50,{22,6,1990}};
int main(int argc, char *argv[])
{
printf("\nEmployee Name : %s",emp.ename);
printf("\nEmployee SSN : %d",emp.ssn);
printf("\nEmployee Salary : %f",emp.salary);
printf("\nEmployee DOJ : %d/%d/%d", \
emp.doj.date,emp.doj.month,emp.doj.year);
return 0;
}
Output :
Employee Name : Pritesh
Employee SSN : 1000
Employee Salary : 1000.500000
Employee DOJ : 22/6/1990