Sobes.tech
Junior — Middle

What is the difference between declaring variables using structures and the keyword const in a programming language?

sobes.tech AI

Answer from AI

Declaring variables in C/C++ using structures and the const keyword serve different purposes.

  • Structures (struct) are user-defined data types that group several variables (fields) under one name. They allow creating complex objects with different fields.

  • const is a qualifier that indicates that the value of a variable cannot be changed after initialization.

Example:

struct Point {
    int x;
    int y;
};

const int a = 5; // a cannot be changed

const Point p = {1, 2}; // fields p.x and p.y cannot be changed

Thus, struct defines a data structure, while const restricts the mutability of a variable or object.