[C++/C] Thorough Explanation of the Difference Between Pointer (const char*) and Array (char[])

目次

Introduction

When handling C-style strings in C++ or C, there are two very similar methods:

  • Using a Pointer: const char* ptr = "text";
  • Using an Array: char arr[100];

These are similar yet distinct, and using them without understanding the differences in their behavior can lead to unexpected errors and bugs. Specifically, there are decisive differences regarding “reassignment” and “modification of content.”

In this article, I will explain the fundamental differences between pointers and arrays when handling strings, using diagrams and examples.

1. The Case of Pointer (const char*)

const char* is a pointer that points to the start address of a read-only memory area where a “string literal (constant)” is stored.

Features

  • Can change where it points: The pointer variable itself can be reassigned to point to a different address.
  • Cannot change the content it points to: Since string literals are read-only, trying to rewrite the content via the pointer is prohibited.

Sample Code

#include <iostream>

using namespace std;

int main() {
    // The message pointer points to the location of the string literal "Initial Text"
    const char* message = "Initial Text";
    
    cout << "Content pointed to initially: " << message << endl;

    // OK: Change the message pointer to point to a different string literal, "New Text"
    message = "New Text";
    
    cout << "Content pointed to after change: " << message << endl;

    // NG: Trying to change the content being pointed to results in a compile error or runtime error
    // message[0] = 'h'; 

    return 0;
}

Visual Analogy

The operation message = "New Text"; is like changing the direction of the arrow named message to point to a block called “New Text” located elsewhere. The original “Initial Text” remains as it is.

2. The Case of Array (char[])

char arr[100]; is a declaration that “allocates a read/write memory area for 100 characters.”

Features

  • Cannot change where it points: The array variable name (arr) acts like a “constant” that indicates the start address of the allocated memory area. It cannot be reassigned to point elsewhere.
  • Can freely change the allocated content: You can copy (overwrite) a new string into the allocated memory area using cin or strcpy.

Sample Code

#include <iostream>
#include <cstring> // Required to use strcpy

using namespace std;

int main() {
    // Allocate a read/write area for 100 characters
    char buffer[100];
    
    // OK: Use strcpy to overwrite the contents of the array with "First message"
    strcpy(buffer, "First message");
    cout << "Initial array content: " << buffer << endl;
    
    // OK: Use cin to overwrite the contents of the array with input from the keyboard
    cout << "Please enter a new message: ";
    cin >> buffer;
    cout << "Array content after input: " << buffer << endl;

    // NG: You cannot assign (reassign) a different string literal to the array variable itself
    // buffer = "Another message"; // Compile Error!

    return 0;
}

Visual Analogy

The operation strcpy(buffer, ...) is like overwriting the contents of a fixed box named buffer with something new. The location of the box itself does not change.

Summary

FeaturePointer (const char*)Array (char[])
ReassignmentPossible (str = "..."; Points to a new location)Impossible (Box location is fixed)
Content ModificationImpossible (str[0]=..; Read-only)Possible (Box contents are writable)

Understanding this difference is crucial for safely handling C-style strings. Note that in modern C++, it is strongly recommended to use the std::string class, which resolves these complexities and allows for safer string handling.

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

この記事を書いた人

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

目次