可以使用指针来查看字符串的地址值。例如:

#include <iostream>
using namespace std;

int main() {
    char str[] = 'Hello, world!';
    char* ptr = str;

    cout << 'Address of str: ' << &str << endl;
    cout << 'Address of ptr: ' << &ptr << endl;
    cout << 'Value of ptr: ' << (void*)ptr << endl;

    return 0;
}

输出:

Address of str: 0x7ffce4e4d4c0
Address of ptr: 0x7ffce4e4d4b8
Value of ptr: 0x7ffce4e4d4c0

其中,&str 表示字符串数组 str 的地址,&ptr 表示指针变量 ptr 的地址,(void*)ptr 表示将指针变量 ptr 的值转换为 void 类型后输出。由于指针变量在内存中存储的是指向的内存地址,因此输出的是字符串数组 str 的地址值。

C++ 获取字符串地址:指针与地址操作

原文地址: https://www.cveoy.top/t/topic/no9E 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录