×
>
<

C Programming

C Structure | CrackEase

Structure

Introduction to structure

A structure (declared with the struct keyword) is a user-defined composite data type that groups together variables of different types under a single name. Each item inside a structure is called a member.

Structures are useful to represent complex data — for example, you can store a student's name (string), id (integer), age (integer) and GPA (float) together in one structure variable.

Unlike arrays (which hold items of the same type), structures let you combine different types in one logical unit.

Structure — quick notes

After you define a structure type you can declare variables of that type (just like built-in types). You can also create arrays of structures. Structure definitions usually appear before main() or in a header file.

Definition

Defining a structure

Use the struct keyword followed by the structure name and a block that lists members. End with a semicolon.

  
struct struct_Name
{
   data_type member1;
   data_type member2;
   ...
};
  

Example :

  
struct Student
{
   int  stu_id;
   char stu_name[50];
   int  stu_age;
   char stu_branch[20];
};
  

Here the structure Student has four members with different types. Note: string members are character arrays with enough size to hold a name.

Structure in C
c structure
Structure Variables

Declaring structure variables

You can declare structure variables in two common ways:

  1. Declare variables right after the structure definition.
  2. Declare variables later inside a function (e.g. main()).

Syntax — declare variables with the definition

  
struct StructureName
{
   data_type member1;
   data_type member2;
} structure_var1, structure_var2;
  

Example:

  
struct Student
{
   int  stu_id;
   int  stu_age;
   char stu_name[50];
   char stu_branch[20];
} info;
  

Syntax — declare a variable inside main()

  
struct Student
{
   int  stu_id;
   int  stu_age;
   char stu_name[50];
   char stu_branch[20];
};

int main(void)
{
    struct Student info;
    /* use info... */
    return 0;
}
  

Accessing structure members

Use the member access operator . to access or assign a member: structure_var.member.

Example:

  
#include <stdio.h>
#include <string.h>

struct Student
{
    char stu_name[50];
    int  stu_age;
    char stu_branch[20];
    int  stu_id;
};

int main(void)
{
    struct Student s1;    /* s1 is a variable of type struct Student */
    s1.stu_age = 17;      /* assign age */

    /* copy a string into the name member */
    strcpy(s1.stu_name, "Somya");

    printf("Name of student 1: %s\n", s1.stu_name);
    printf("Age of student 1 : %d\n", s1.stu_age);

    return 0;
}
  
output
  
Name of student 1: Somya
Age of student 1 : 17
  

Structure initialization

You can initialize structure variables at compile time (aggregate initializer) or assign members individually.

Type 1 — aggregate initialization

  
struct Student
{
  float height;
  int   weight;
  int   age;
};

struct Student s1 = { 180.75f, 73, 23 };  /* height, weight, age */
  

Type 2 — initialize members individually

  
struct Student s1;
s1.height = 182.5f;
s1.weight = 65;
s1.age    = 20;
  

Structure size (sizeof)

The size of a structure is at least the sum of sizes of its members but can include padding for alignment. Use sizeof() to find the size in bytes.

Example:

  
#include <stdio.h>
#include <string.h>

struct Student
{
    int  stu_id;
    char stu_name[10];
};

int main(void)
{
    struct Student info;

    printf("size of stu_id: %zu bytes\n", sizeof(info.stu_id));
    printf("size of stu_name: %zu bytes\n", sizeof(info.stu_name));
    printf("size of struct Student: %zu bytes\n", sizeof(info));
    return 0;
}
  
example output (platform-dependent)
  
size of stu_id: 4 bytes
size of stu_name: 10 bytes
size of struct Student: 16 bytes
  

Array of structures

Just like primitive types, you can create arrays of structures where each element is a full structure instance.

Example :

  
#include <stdio.h>

struct Student
{
    char name[50];
    int  id;
};

int main(void)
{
    struct Student stu[3]; /* array of 3 students */

    for (int i = 0; i < 3; ++i)
    {
        printf("Enter name for student %d: ", i+1);
        scanf("%49s", stu[i].name);   /* limit input to avoid overflow */
        printf("Enter id for student %d: ", i+1);
        scanf("%d", &stu[i].id);
    }

    printf("\nDisplaying student records:\n");
    for (int i = 0; i < 3; ++i)
    {
        printf("Student %d name: %s\n", i+1, stu[i].name);
        printf("Student %d id  : %d\n", i+1, stu[i].id);
    }
    return 0;
}
  
Footer Content | CrackEase