ProgramGuru

C Structures and Student Records

C Structures and Student Records

A student has an ID, name and mark with different types. Keeping separate unrelated variables makes it easy to pair one student’s ID with another student’s mark.

A struct groups named members into one record type. Use a dot for a structure object and -> for access through a pointer to a structure.

  1. Explain

    Create and update a student record while preserving its field meanings.

  2. Trace

    Copy the record, change only the copied mark to 50 and print both marks.

  3. Apply

    Distinguish updating a copied record from updating the original record.

Structures: follow the data

  1. Start

    A student has an ID, name and mark with different types. Keeping separate unrelated variables makes it easy to pair one student’s ID with another student’s mark.

  2. Operation

    A struct groups named members into one record type. Use a dot for a structure object and -> for access through a pointer to a structure.

  3. Result

    7 Asha 45

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

The structure declaration describes a type; struct Student student creates an object of that type. Its members can have different types and carry names that explain their responsibilities.

An initialized character-array member stores its own text. Structure assignment copies the member values, including embedded arrays; copying a pointer member would copy the pointer rather than separately duplicating its pointed-to storage.

Read the complete C program

This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.

C17 · complete program

#include <stdio.h>
struct Student {
  int id;
  char name[16];
  int mark;
};
int main(void) {
  struct Student student = {7, "Asha", 40};
  student.mark = 45;
  printf("%d %s %d\n", student.id, student.name, student.mark);
  return 0;
}

Expected output

7 Asha 45
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
struct Student {
  int id;
  char name[16];
  int mark;
};
int main(void) {
  struct Student student = {7, "Asha", 40};
  student.mark = 45;
  printf("%d %s %d\n", student.id, student.name, student.mark);
  return 0;
}

Initialization pairs ID 7, name Asha and mark 40 in one object.

The mark member changes to 45, while the ID and name remain attached to the same record.

Follow the worked calculation

  1. Read the starting values

    Initialization pairs ID 7, name Asha and mark 40 in one object.

  2. Perform the operation

    The mark member changes to 45, while the ID and name remain attached to the same record.

  3. Check the observable result

    7 Asha 45

    7 Asha 45
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
struct Student {
  int id;
  char name[16];
  int mark;
};
int main(void) {
  struct Student student = {7, "Asha", 40};
  student.mark = 45;
  printf("%d %s %d\n", student.id, student.name, student.mark);
  return 0;
}

Copy the record, change only the copied mark to 50 and print both marks.

Worked answer

These are two structure objects containing separate integer members. Updating the copied integer does not modify the original. That conclusion would need more care for pointed-to data in pointer members.

C17 · complete program

#include <stdio.h>
struct Student {
  int id;
  char name[16];
  int mark;
};
int main(void) {
  struct Student original = {7, "Asha", 40};
  struct Student copy = original;
  copy.mark = 50;
  printf("Original: %d; copy: %d\n", original.mark, copy.mark);
  return 0;
}

Expected output

Original: 40; copy: 50
Run in C compiler →

Recognize the important boundaries

Structures may contain padding. Do not assume sizeof a structure equals the sum of its member sizes or write raw structure bytes as a portable file format.

Passing a large structure by value copies its value. A pointer can avoid that copy but introduces explicit lifetime and access responsibilities.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Does assigning one structure to another deeply copy storage reached through pointer members?
Choose one answer. You can try again.

Answer and explanation

No; pointer members are copied as pointer values.

Correct. Any pointed-to allocation is not automatically duplicated.

Use the idea independently

A struct groups named members into one record type. Use a dot for a structure object and -> for access through a pointer to a structure.

Continue with allocation and ownership.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Dynamic Memory, Allocation Failure and Ownership →