Curriculum
Course: Complete C++ Programming Course
Login

Curriculum

Complete C++ Programming Course

Text lesson

A Tour of Structure in C

In this lesson, you will learn how to store different types of data (such as float, char, and int) in a single unit(name) called Structures.

  • What are Structures
  • Syntax of Structure Definition
  • Defining the Structure
  • Creating s Structure Variable
  • Accessing the Structure Variable
  • A Simple Example Using Structure
  • Initialization of Structure Variable
  • Passing Structure to a Function
  • Returning a Structure from Function

 

What is Structure

  • A structure (or struct) is a user-defined data type that allows grouping variables of different data types under a single name.
  • A structure is a collection of simple variables. The variables in a structure can be of different types: Some can be int, some can be float, and so on.
  • The data items in a structure are called the members of the structure.

 

Syntax of Structure Definition

The following figure shows the syntax of the structure declaration

 

Example – Defining the Structure

This tells how the structure is organized and it specifies what members the structure will have.

struct part
{
   int modelnumber;
   int partnumber;
   float cost;
};

 

This structure part represents an item in a widget company’s parts inventory. The structure is a kind of blueprint specifying what information is necessary for a single part.

It contains three variables: two integers and a floating-point number

Note: The structure definition serves only as a blueprint for the creation of variables of the type part. It does not take any space in memory.

 

Creating a Structure Variable

Syntax: <structure-name> structure-variable;

Example:

part part1;

Here, part 1 is the variable of the structure part. It reserves space in memory for part 1.

How much space?

In this case, there will be 4 bytes for each of the two ints (assuming a 32-bit system), and 4 bytes for the float.

Note!

In some ways, we can think of the part structure as the specification for a new data type.

The format for defining a structure variable is the same as that for defining a basic built-in data type such as int.

part part1;
int var1;

C++ makes the syntax and the operation of user-defined data types as similar as possible to that of built-in data types.

In C you need to include the keyword struct in structure definitions, as in struct part part1; In C++ the keyword ‘struct’ is not necessary.

 

Accessing the Structure Variables

Once a structure variable has been defined, its members can be accessed using the dot operator.

For Example: part1.modelnumber = 6244;

 

This means modelnumber is a member of part1. The real name of the dot operator is the member access operator.

Displaying the member variable of a structure

cout << "nModel " << part1.modelnumber;

 

Example: Creating Structure and Accessing Member Variables

//StructureEx2.cpp
// uses parts inventory to demonstrate structures
#include <iostream>
using namespace std;

struct part //declare a structure
{
    int modelnumber; //ID number of widget
    int partnumber; //ID number of widget part
    float cost; //cost of part
};

int main()
{
    part part1; //define a structure variable
    part1.modelnumber = 6244; //give values to structure members
    part1.partnumber = 373;
    part1.cost = 217.55F;
    //display structure members
    cout << "Model " << part1.modelnumber;
    cout << ", part " << part1.partnumber;
    cout << ", costs $" << part1.cost << endl;
    return 0;
}

Output

Model 6244, part 373, costs $217.55

Note: We can create multiple variables of the structure, and each variable reserves a separate space in memory.

 

Example: Creating Multiple(3) Structure Variables.

part part1, part2, part3;

Here, part1, part2, and part3 are three different structure variables and reserve a separate memory space for all data members.

The following figure shows the memory representations of structure variables part1, part2, and part3.

 

Do multiple structure objects have a continuous address?

No, multiple structure objects do not always have continuous memory addresses. While the members within a single structure are stored contiguously, the addresses of separate structure instances are not guaranteed to be adjacent in memory

Within a Structure:
The members of a structure are stored one after another in memory(contiguous), with the order determined by how they are declared in the structure definition.

 

Initialization of Structure Variables

We can initialize the structure variable at the time of creation also. Refer to Example 1 and Example 2.

Method 1: Initialization of Structure Variables

struct part
{
   int modelnumber;
   int partnumber;
   float cost;
};

int main()
{
   part part1 = { 6244, 373, 217.55F };
}

Method-2: 

struct part //declare a structure
{
    int modelnumber; //ID number of widget
    int partnumber; //ID number of widget part
    float cost; //cost of part
} part1 = {6244, 373, 217.55F},part2;

 

Example – Initialization of Structure Variables

//StructureEx3.cpp
//Showing initialization of structure variables
#include <iostream>
using namespace std;

struct part //specify a structure
{
    int modelnumber; //ID number of widget
    int partnumber; //ID number of widget part
    float cost; //cost of part
};

int main()
{ //initialize variable
    part part1 = { 6244, 373, 217.55F };
    part part2; //define variable
    
    //display first variable
    cout << "Model " << part1.modelnumber;
    cout << ", part " << part1.partnumber;
    cout << ", costs $" << part1.cost << endl;
    
    part2 = part1; //assign first variable to second
    //display second variable
    cout << "Model " << part2.modelnumber;
    cout << ", part " << part2.partnumber;
    cout << ", costs $" << part2.cost << endl;
    return 0;
}

Output

Model 6244, part 373, costs $217.55
Model 6244, part 373, costs $217.55

 

The value of each member of part 1 is assigned to the corresponding member of part 2. Since a large structure can have dozens of members, such an assignment statement can require the computer to do a considerable amount of work.

 

Note: One structure variable can be assigned to another only when they are of the same structure type.

 

Passing Structure to a Function

In C programming, a struct (structure) can be passed to a function in two main ways:

1. Pass by Value (Copy of Structure)

  • A copy of the structure is passed to the function.

  • Changes made inside the function do not affect the original structure.

2. Pass by Reference (Using Pointers)

  • A pointer to the structure is passed.

  • Changes made inside the function do affect the original structure.

 

Example with Both Methods

#include<stdio.h>

// Define a structure
struct Student {
    int roll;
    float marks;
};

// Function: Pass by value
void displayByValue(struct Student s) {
    printf("Inside displayByValue:\n");
    printf("Roll: %d, Marks: %.2f\n", s.roll, s.marks);
    s.marks = 100.0;  // This change won't affect the original
}

// Function: Pass by reference
void displayByReference(struct Student* s) {
    printf("Inside displayByReference:\n");
    printf("Roll: %d, Marks: %.2f\n", s->roll, s->marks);
    s->marks = 100.0;  // This change will affect the original
}

int main() {
    struct Student stu = {101, 85.5};

    // Pass by value
    displayByValue(stu);
    printf("After displayByValue: Marks = %.2f\n", stu.marks);

    // Pass by reference
    displayByReference(&stu);
    printf("After displayByReference: Marks = %.2f\n", stu.marks);

    return 0;
}

Output

Inside displayByValue:
Roll: 101, Marks: 85.50
After displayByValue: Marks = 85.50
Inside displayByReference:
Roll: 101, Marks: 85.50
After displayByReference: Marks = 100.00

 

Returning a Structure from a Function

1. Return Structure by Value

The function returns a copy of the structure.

2. Return Structure by Reference (using Pointer)

The function returns a pointer to a structure (useful when the structure is large or when you want to modify the original).

Example: Returning Structure by Value and Pointer

#include<stdio.h>
#include<stdlib.h>

// Define a structure
struct Student {
    int roll;
    float marks;
};

// Function 1: Return structure by value
struct Student getStudentByValue() {
    struct Student s;
    s.roll = 101;
    s.marks = 88.5;
    return s;  // Return a copy of the structure
}

// Function 2: Return structure by pointer
struct Student* getStudentByPointer() {
    struct Student* s = (struct Student*)malloc(sizeof(struct Student));
    s->roll = 102;
    s->marks = 92.3;
    return s;  // Return pointer to dynamically allocated structure
}

int main() {
    // Using function that returns by value
    struct Student s1 = getStudentByValue();
    printf("Returned by Value:\n");
    printf("Roll: %d, Marks: %.2f\n\n", s1.roll, s1.marks);

    // Using function that returns by pointer
    struct Student* s2 = getStudentByPointer();
    printf("Returned by Pointer:\n");
    printf("Roll: %d, Marks: %.2f\n", s2->roll, s2->marks);

    // Free dynamically allocated memory
    free(s2);

    return 0;
}

 

Output:

Returned by Value:
Roll: 101, Marks: 88.50

Returned by Pointer:
Roll: 102, Marks: 92.30


 

 

 

Student Ratings and Reviews

 

4.8
4.8 out of 5 stars (based on 4 reviews)
Excellent
Very good
Average
Poor
Terrible

 

 

6 October 2025

Nice

Saifhussain01493
1 September 2025

Helping a lot….

aanaand9573
12 August 2025

Crystal clear

Aishika
28 August 2024

good explanation

Ayush Ramola

 

Submit a Review