c ++中int*和char*之间的区别

g4u*_*r4v 1 c++ pointers

#include <iostream>
using namespace std;

int main() {
    int * a[5];
    char * b[5];
    cout<<a[1]; // this works and prints address being held by second element in the array  
    cout<<b[1]; // this gives run time error . why ?
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以向我解释cout<<b[1]给出运行时错误?int和char数组的行为是否应该彼此相似?

Lig*_*ica 6

因为IOStreams专门用于治疗char*.

char* 通常指向一个C字符串,所以IOStreams只会假设他们这样做并取消引用它们.

你的没有.