You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++中字符串数组指针的正确定义方式及写法差异疑问

C++字符串数组指针的定义与区别

Great question—let's break this down step by step to clarify each part of your inquiry.

1. std::string* str_arr_p[]; 是否合法?是否必须指定数组大小?

First off, let's parse what this declaration actually means: std::string* str_arr_p[] defines an array of pointers to std::string objects (often called a "pointer array").

Whether you need to specify the size depends on where you declare it:

  • Global/namespace scope or static class members: You can omit the size here. The compiler will let you declare it without a size, as long as you provide an initializer later (either in the same translation unit or via extern). For example:
    // Global scope declaration (no size specified)
    std::string* str_arr_p[];
    
    // Later in the same file, define and initialize it
    std::string s1 = "foo", s2 = "bar";
    std::string* str_arr_p[] = {&s1, &s2}; // Compiler infers size as 2
    
  • Local scope (e.g., inside a function): You must specify the size, or provide an initializer that lets the compiler infer it. If you just write std::string* str_arr_p[]; without a size or initializer in a function, the compiler will throw an error—it can't allocate memory for an array of unknown size on the stack. Example of valid local usage:
    void my_function() {
        std::string a = "hello", b = "world";
        // Valid: size inferred from initializer
        std::string* str_arr_p[] = {&a, &b};
        // Also valid: explicitly specify size
        std::string* str_arr_p2[2] = {&a, &b};
    }
    

2. std::string* str_arr_p[n]; vs std::string (*str_arr_p)[n]; — 核心区别

The key here is operator precedence: the [] operator has higher priority than the * operator. Let's break each down:

std::string* str_arr_p[n];

This is an array of n pointers to std::string. Each element in the array holds the address of a single std::string object. Think of it as a collection of independent string pointers.

Usage example:

std::string str1 = "apple", str2 = "banana";
std::string* ptr_array[2] = {&str1, &str2};

// Access the first string via the pointer array
std::cout << *ptr_array[0] << std::endl; // Outputs "apple"

std::string (*str_arr_p)[n];

This is a pointer to an array of n std::string objects (often called an "array pointer"). The variable itself is a single pointer that points to the entire array of strings, not individual pointers.

Usage example:

std::string str_array[2] = {"cat", "dog"};
std::string (*array_ptr)[2] = &str_array;

// Access the first string via the array pointer
// Note: We need to dereference the pointer first to get the array, then index into it
std::cout << (*array_ptr)[0] << std::endl; // Outputs "cat"

To sum up:

  • std::string* str_arr_p[n]; = Array of n string pointers
  • std::string (*str_arr_p)[n]; = Pointer to an array of n strings

内容的提问来源于stack exchange,提问作者Alex Fateev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:02:42