...

/

Structures and Memory

Structures and Memory

Explore how structures are stored in memory with the help of an interactive example.

How are structures stored in memory?

Structure members are stored in consecutive memory locations. The size of the structure variable is at least equal to the sum of sizes of structure elements.

Example program

See the code given below!

Press + to interact
# include <stdio.h>
int main( )
{
// Declares structure employee
struct employee
{
// Members of structure
char name ;
int age ;
float salary ;
} ;
// Initalizes structure variable e1
struct employee e1 = { 'A', 23, 4000.50 } ;
// Address of structure members
printf ("%u %u %u\n", &e1.name, &e1.age, &e1.salary);
// Size of structure members
printf ("%d %d %d\n", sizeof(e1.name), sizeof(e1.age), sizeof(e1.salary));
// Size of structure variable
printf ("%d\n", sizeof(e1));
return 0;
}

Explanation

From the above code, you can see that ...