vs中char类型变量取地址为乱码
代码:
char test;
std::cout<<"address for test:"<<&test<<std::endl;结果:
烫烫烫烫烫烫烫烫烫烫?

解决方案 »

  1.   


    std::cout <<"address for test:" <<(void*)&test <<std::endl;因为cout对于char*类型的直接输出内容,不是输出地址
      

  2.   

    char test; 
    std::cout < <"address for test:" < <(int)&test < <std::endl; 
      

  3.   

    还有个问题,原来的代码:
    struct testMemory
    {
    int firstInt;
    char secondChar;
    long thirdLong;
    double lastDouble;
    };
    int _tmain(int argc, _TCHAR* argv[])
    {
    testMemory tem;
    std::cout<<"address for tem's firstInt:"<<&tem.firstInt<<" sizeof(int)="<<sizeof(tem.firstInt)<<std::endl;
    std::cout<<"address for tem's secondChar:"<<((int)&tem.secondChar)<<" sizeof(char)="<<sizeof(tem.secondChar)<<std::endl;
    std::cout<<"address for tem's thirdLong:"<<&tem.thirdLong<<" sizeof(long)="<<sizeof(tem.thirdLong)<<std::endl;
    std::cout<<"address for tem's lastDouble:"<<&tem.lastDouble<<" sizeof(double)="<<sizeof(tem.thirdLong)<<std::endl;
    system("pause");
    return 0;
    }
    结果:
    address for tem's firstInt:0012FF4C sizeof(int)=4
    address for tem's secondChar:1245008(0012ff50) sizeof(char)=1
    address for tem's thirdLong:0012FF54 sizeof(long)=4
    address for tem's lastDouble:0012FF5C sizeof(double)=4
    为什么secondChar后面跳了4呢(0012FF54-0012ff50)他不是占一个字节呢,关于自定义数据类型的具体内存分配是怎么样的呢?
      

  4.   

    结构体呀,你google 内存对齐.