Skip to main content

Overview of Structures in C

What is Structure

  • A structure is a composite data type that allows you to group together variables of different data types under a single name.

  • A structure is defined using the struct keyword, followed by a user-defined name for the structure, and a list of one or more member variables, each of which has its own data type.

Syntax of Structure

The syntax for defining a structure in C is as follows:

struct structure_name {
data_type member1;
data_type member2;
...
data_type memberN;
};

Explanation

The structure_name is a user-defined name for the structure, and member1 through memberN are the member variables of the structure. Each member variable is defined with a data type, just like any other variable.

Example of Structure

  • The following code defines a simple structure called person that has three member variables: name (a character array), age (an integer), and height (a float).
struct person {
char name[50];
int age;
float height;
};
  • Once a structure is defined, you can create variables of that structure type, just like you would create variables of any other data type.

  • You can then access the member variables of the structure using the dot operator (.) to separate the structure variable name and the member name.

For example:

The following code creates a variable of the person structure type and initializes its member variables:

struct person john;
strcpy(john.name, "John Smith");
john.age = 35;
john.height = 1.8;

Structure Properties

A structure type in C has the following properties:

Member variables

  • A structure is composed of one or more member variables, each of which has its own data type.
  • Member variables can be of any C data type, including basic types (such as int and float), pointers, arrays, and even other structures.

Memory layout

  • The memory layout of a structure is contiguous, which means that all member variables are stored in a single block of memory.
  • The size of a structure is determined by the sum of the sizes of its member variables, plus any padding that may be added to align the data on memory boundaries.

Accessing members

You can access the individual member variables of a structure using the dot operator (.) to separate the structure variable name and the member name.

For example:

If you have a structure called person with a member variable called name, you can access the name variable of a structure instance p like this: p.name.

Initializing

You can initialize a structure variable at the time it is declared by providing a comma-separated list of values for its member variables in braces.

For example:

To initialize a person structure with a name of "John" and an age of 25, you could write: struct person p = {"John", 25};

Passing as argument

You can pass a structure variable as an argument to a function by value or by reference (using a pointer to the structure).

Returning from function

You can also return a structure from a function, either by value or by reference.

Example of using Stuctures

Here's a basic program using structures in C that creates a person structure type, initializes a variable of that type, and prints out the member variables of that variable:

#include <stdio.h>

struct person
{
char name[50];
int age;
float height;
};

int main()
{
struct person john = {"John Smith", 35, 1.8};
printf("Name: %s\n", john.name);
printf("Age: %d\n", john.age);
printf("Height: %.2f\n", john.height);
return 0;
}
Output:

Explanation

  • We define a person structure type with three member variables: name, age, and height.
  • We then declare a variable john of type person and initialize its member variables using a brace-enclosed list.
  • Finally, we print out the values of john's member variables using printf() statements.