相关疑难解决方法(0)

cout << with char*参数打印字符串,而不是指针值

这个:

const char * terry = "hello";
cout<<terry;
Run Code Online (Sandbox Code Playgroud)

打印hello而不是的内存地址'h'.为什么会这样?

c++

55
推荐指数
4
解决办法
8万
查看次数

vector <char>元素的打印地址显示垃圾

考虑:

#include <vector>
#include <string>
#include <iostream>
using namespace std;

int main()
{
    vector<char> vChar;
    vChar.push_back('a');
    vChar.push_back('b');
    vChar.push_back('c');
    vChar.push_back('d');

    vector<int> vInt;
    vInt.push_back(1);
    vInt.push_back(2);
    vInt.push_back(3);
    vInt.push_back(4);

    cout << "For char vector Size:" << vChar.size() << " Capacity:" << vChar.capacity() << "\n";
    for(int i=0; i < vChar.size(); i++)
    {
        cout << "Data: " << vChar[i] << " Address:" <<  &vChar[i]  << "\n";
    }

    cout << "\nFor int vector Size:" << vInt.size() << " Capacity:" << vInt.capacity() << "\n";
    for (int i = …
Run Code Online (Sandbox Code Playgroud)

c++ vector c++17

21
推荐指数
2
解决办法
2072
查看次数

打印C++ int指针vs char指针

当我运行以下代码时:

int i[] = {1,2,3};
int* pointer = i;
cout << i << endl;

char c[] = {'a','b','c','\0'};
char* ptr = c;
cout << ptr << endl;
Run Code Online (Sandbox Code Playgroud)

我得到这个输出:

0x28ff1c
abc
Run Code Online (Sandbox Code Playgroud)

为什么int指针返回地址,而char指针返回数组的实际内容?

c++ string pointers

2
推荐指数
1
解决办法
230
查看次数

为什么字符串表现不同?

#include <iostream>

#include <string.h>

using namespace std;

int main()

{

    char x[100]="hello";

    int s=strlen(x);

    cout<<&(x[0]);

}
Run Code Online (Sandbox Code Playgroud)

如果我编译并运行它,

输出是你好的

为什么输出不是字符'h'的地址?

c++ string char

0
推荐指数
1
解决办法
115
查看次数

标签 统计

c++ ×4

string ×2

c++17 ×1

char ×1

pointers ×1

vector ×1