//
// stream.cpp
// C++primary
//
// Created by amesty on 16/5/25.
// Copyright © 2016年 travelrely. All rights reserved.
//
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
char*ch="hello world";
cout<<(void*)ch<<endl;
cout<<&ch<<endl;
cout<<ch<<endl;
char ch1[12]="hello world";
cout<<(void*)ch1<<endl;
cout<<&ch1<<endl;
cout<<ch1<<endl;
//iostream把char *理解为字符串,直接输出C风格的字符串内容,如果要输出地址形势的指针内容,转换为void *类型,它就只好输出地址了
//void *有时候又称为泛型指针,也就是可以指向任何类型的数据的指针,在大多数仅仅把一块内存看作raw memeory,也就是没有任何类型信息的时候,就会把他定义为void *,编译器对他的处理就是一个字节一个字节处理,类似于unsigned char *的处理方式,当然你的char类型必须是1个字节才行。
// 在C的标准库中关于memory的操作<mem.h>几乎都是使用的void *类型,将类型的转换交给程序员处理,否则相同的功能仅仅是因为数据类型不一样,就要提供一个函数,而偏偏C又不支持函数重载。
char*p="你";
if ((unsigned char)*p > 127) //汉字
{
cout<<"是汉字\n";
}
else //标准的ASCII字符
{
cout<<"不是汉字\n";
}
char*ch3="hello world";
char ch4[12]="hello world";
cout<<ch3<<ch4<<endl;
return 0;
}
======================
0x100002c10
0x7fff5fbff8e0
hello world
cout<<(void*)ch<<endl;
cout<<&ch<<endl;
这两个地址不同:
0x7fff5fbff96c
0x7fff5fbff96c
hello world
是汉字
hello worldhello world
Program ended with exit code: 0