[C++/C] Introduction to Struct Pointers | Getting Addresses (&) and Accessing Members

目次

Introduction

One of the powerful features of C++ and C is “pointers.” A pointer is a special variable used to hold the memory address where a variable is stored.

You can also use this pointer mechanism for structures. By declaring a “pointer to a structure” and assigning the address of an existing structure variable, you can manipulate that structure indirectly. This is a very important technique, especially when you want to pass large structures to functions efficiently.

In this article, I will explain the basic methods for declaring a pointer to a structure and setting the address of an existing structure variable using the address operator &.

Sample Code for Struct Pointers

This code creates a variable book1 of the Book structure. Next, it declares a pointer p_book pointing to the Book type and assigns the memory address of book1 to it.

Complete Code

#include <iostream>
#include <string>

// Definition of "Book" structure
struct Book {
    std::string title; // Title
    int price;         // Price
};

int main() {
    // 1. Create a structure variable (instance)
    Book book1;
    book1.title = "Introduction to C++ Programming";
    book1.price = 2800;
    
    // 2. Declare a pointer pointing to the structure
    Book* p_book;
    
    // 3. Assign the address of book1 to the pointer using the address operator (&)
    p_book = &book1;

    // 4. Display information of the original structure through the pointer
    //    (Member access via pointers will be explained in the next article)
    std::cout << "Title of the book pointed to by p_book: "
              << p_book->title << std::endl;
              
    std::cout << "Memory address of variable book1: " << &book1 << std::endl;
    std::cout << "Address stored in pointer p_book: " << p_book << std::endl;
    
    return 0;
}

Execution Result (Address values vary by environment)

Plaintext

Title of the book pointed to by p_book: Introduction to C++ Programming
Memory address of variable book1: 0x7ffc316a7b20
Address stored in pointer p_book: 0x7ffc316a7b20

Code Explanation

Book* p_book;

This part declares a pointer pointing to a structure.

  • Book: Indicates that the data type the pointer points to is the Book type.
  • *: The asterisk indicates that this variable p_book is a pointer variable that stores an address, rather than a normal variable that stores a value directly.

Important: At this point, the pointer p_book does not point anywhere (it contains an indefinite address). Please note that simply declaring a pointer does not create an instance (actual body) of the structure.

p_book = &book1;

This is the core part that links the pointer to the existing structure variable.

  • &book1: The address operator & retrieves the “address information” of where the variable book1 is located in memory.
  • p_book = …: The retrieved address of book1 is assigned to the pointer variable p_book.

With this operation, p_book is now in a state where it “points to” book1. It becomes possible to read and write the contents of book1 through p_book.

Summary

In this article, I explained the declaration of pointers to structures and the assignment of addresses.

  • Declare a pointer by adding an asterisk *, like StructName* pointerName;.
  • Retrieve the memory address of an existing structure variable using the & operator.
  • Assign the retrieved address to the pointer, like pointer = &variable;.

Understanding this basic concept of “pointing to an address” is the key to mastering C++ and C.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次