字符串常量

 

用常量字符串去赋值给指针和初始化数组的时候是不一样的。

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){

    char str1[] = "hello world";
    char str2[] = "hello world";
    char *str3 = "hello world";
    char *str4 = "hello world";
    if(str1 == str2){
        printf("str1 and str2 are same.\n");
    }else{
        cout<< static_cast<const void *>(str1)<<" "<<static_cast<const void *>(str2)<<endl;
        printf("str1 and str2 are not same.\n");
    }
    if(str3 == str4){
        cout<<static_cast<const void *>(str3)<<" "<<static_cast<const void *>(str4)<<endl;
        printf("str3 and str4 are same.\n");
    }else{
        printf("str3 and str4 are not same.\n");
    }
    return 0;
}

  

posted @ 2016-06-20 21:50  tcgoshawk  阅读(182)  评论(0编辑  收藏  举报