Bird
Raised Fist0
C++programming~5 mins

Accessing structure members in C++

Choose your learning style10 modes available
Introduction

Structures group related data together. Accessing members lets you use or change each piece inside the group.

You want to store information about a person, like name and age, in one place.
You need to keep details of a book, such as title and author, together.
You are working with a point in 2D space and want to access its x and y coordinates.
You want to organize data about a car, like model and year, in a single variable.
Syntax
C++
struct StructureName {
    dataType member1;
    dataType member2;
    // more members
};

StructureName variableName;
variableName.member1 = value;
value = variableName.member2;

Use the dot . operator to access members of a structure variable.

Each member can be accessed or changed individually.

Examples
Defines a Person structure and sets age and height using dot operator.
C++
struct Person {
    int age;
    float height;
};

Person p;
p.age = 25;
p.height = 5.9f;
Creates a Point with x=10 and y=20, then reads x value.
C++
struct Point {
    int x;
    int y;
};

Point pt = {10, 20};
int xValue = pt.x;
Sample Program

This program creates a Car structure, assigns values to its members, and prints them.

C++
#include <iostream>
#include <string>
using namespace std;

struct Car {
    string model;
    int year;
};

int main() {
    Car myCar;
    myCar.model = "Toyota";
    myCar.year = 2020;

    cout << "Car model: " << myCar.model << "\n";
    cout << "Car year: " << myCar.year << "\n";
    return 0;
}
OutputSuccess
Important Notes

Remember to include #include <iostream> and use std::cout or using namespace std; for printing.

Structure members are accessed with the dot operator only when you have a structure variable, not a pointer.

Summary

Structures group related data under one name.

Use the dot operator . to access or change members.

Each member can be used like a normal variable.

Practice

(1/5)
1. What operator is used to access members of a structure variable in C++?
easy
A. Double colon (::)
B. Arrow operator (->)
C. Colon operator (:)
D. Dot operator (.)

Solution

  1. Step 1: Recall structure member access syntax

    In C++, to access a member of a structure variable, the dot operator (.) is used.
  2. Step 2: Differentiate from pointer access

    The arrow operator (->) is used when accessing members through a pointer, not a direct variable.
  3. Final Answer:

    Dot operator (.) -> Option D
  4. Quick Check:

    Structure member access = dot operator (.) [OK]
Hint: Use dot (.) for variables, arrow (->) for pointers [OK]
Common Mistakes:
  • Confusing dot (.) with arrow (->)
  • Using colon (::) which is for scope resolution
  • Using arrow (->) on non-pointer variables
2. Which of the following is the correct syntax to access member age of a structure variable person?
easy
A. person->age;
B. person::age;
C. person.age;
D. person:age;

Solution

  1. Step 1: Identify the variable type

    Since person is a structure variable (not a pointer), use the dot operator to access members.
  2. Step 2: Check syntax correctness

    Correct syntax is person.age;. The arrow operator (->) is for pointers, colons are invalid here.
  3. Final Answer:

    person.age; -> Option C
  4. Quick Check:

    Direct struct variable uses dot (.) [OK]
Hint: Use dot (.) for struct variables, arrow (->) for pointers [OK]
Common Mistakes:
  • Using arrow (->) on non-pointer variables
  • Using colon (:) or double colon (::) incorrectly
  • Missing semicolon at end
3. What will be the output of the following code?
#include <iostream>
struct Point {
  int x;
  int y;
};

int main() {
  Point p = {3, 4};
  std::cout << p.x + p.y << std::endl;
  return 0;
}
medium
A. 7
B. 34
C. 0
D. Compilation error

Solution

  1. Step 1: Understand structure initialization

    The structure Point is initialized with x=3 and y=4.
  2. Step 2: Calculate the output expression

    The code outputs p.x + p.y which is 3 + 4 = 7.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Add members accessed by dot operator for result [OK]
Common Mistakes:
  • Concatenating numbers as strings (34)
  • Assuming default zero initialization
  • Confusing pointer access syntax
4. Identify the error in the following code snippet:
struct Car {
  int speed;
};

int main() {
  Car c;
  c->speed = 100;
  return 0;
}
medium
A. Missing semicolon after struct definition
B. Using arrow operator (->) on a non-pointer variable
C. Incorrect struct member name
D. No error, code is correct

Solution

  1. Step 1: Check variable type and operator

    The variable c is a struct variable, not a pointer.
  2. Step 2: Identify correct member access

    Using c->speed is invalid here; should use c.speed.
  3. Final Answer:

    Using arrow operator (->) on a non-pointer variable -> Option B
  4. Quick Check:

    Non-pointer struct uses dot (.) [OK]
Hint: Use dot (.) for variables, arrow (->) only for pointers [OK]
Common Mistakes:
  • Using arrow (->) on struct variables
  • Assuming arrow works for all struct access
  • Ignoring pointer vs variable difference
5. Given the following code, how can you correctly access the member name of the structure pointer ptr?
struct Student {
  std::string name;
  int id;
};

Student s = {"Alice", 101};
Student* ptr = &s;
// Access name here
hard
A. ptr->name;
B. ptr.name;
C. ptr::name;
D. *ptr.name;

Solution

  1. Step 1: Understand pointer member access

    When accessing members through a pointer, you can use the arrow operator (->) or dereference then dot ( (*ptr).member ).
  2. Step 2: Check each option

    ptr.name; is invalid syntax for a pointer. ptr->name; is the correct syntax using the arrow operator. ptr::name; incorrectly uses scope resolution operator. *ptr.name; is wrong due to operator precedence; it should be (*ptr).name;.
  3. Final Answer:

    ptr->name; -> Option A
  4. Quick Check:

    Pointer member access = arrow (->) or (*ptr).member [OK]
Hint: Use arrow (->) or dereference then dot ( (*ptr).member ) [OK]
Common Mistakes:
  • Using dot (.) directly on pointer
  • Forgetting to dereference pointer
  • Confusing pointer and variable access
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard