zoukankan      html  css  js  c++  java
  • 指向字符串的指针在printf与cout区别

    根据指针用法* 定义一个指针, &变量地址

    int b = 1;

    int *a = &b;

    *a =1,但对于字符串而言并非如此直接打印指向字符串的指针打印的是地址还是字符串本身,具体看情况

    定义:

    char *m1 = "coconut is lovely";  

    char *m2 = "passion fruit isnice";  

    char *m3 = "craneberry is fine";  

    注:实际声明应该是const char *m1,原因char *背后的含义是:给我个字符串,我要修改它,此处应该是要读取它,具体参考

    测试输出:


    cout 打印*m1:

    cout<<"Now use cout to print *m1="<<*m1<<endl;

    打印输出:

    Now use cout to print *m1=c

    输出m1指向字符串的第一个字符

    cout打印m1:

    cout<<"Now use cout to print m1="<<m1<<endl;

    输出

    Now use cout to print m1=coconut is lovely

    输出m1所指的内容,而不是m1指向的地址

    printf打印%c ", *m1

    printf("Now use printf to print *m1=%c ", *m1);

    输出:

    Now use printf to print *m1=c

    m1指向的字符串的第一位的内容注意是%c而不是%s。因为是字符型,而非字符串型。所以以下表达错误: printf("Now use printf to print *m1= %s ", *m1);

    printf 打印 %d m1

    printf("Now use printf to print m1=%d ", m1);

    输出:

    Now use printf to print m1=4197112

    m1是指针,输出m1所指向的地址。上面例子中的cout<<m1输出的是字符串内容。二者不一致,似乎反常了。但是我们可以使得它们行为一致。如下:

    printf打印%s m1

    printf("Now use printf to print m1= %s ",m1);

    输出

    Now use printf to print m1= coconut is lovely

    m1是指针,输出m1所指向的地址。使%s而非%d就可以使得m1不去表示地址而去表示字符串内容

     

    完整例子:

    #include <stdio.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char *m1 = "coconut is lovely";  
        char *m2 = "passion fruit isnice";  
        char *m3 = "craneberry is fine";
        char* message[3];                   
        int i;                              
    
        //cout *m1
        cout<<"Now use cout to print *m1="<<*m1<<endl; 
        //cout m1
        cout<<"Now use cout to print m1="<<m1<<endl;
        //cout (int)m1: 64位机char*类型大小位8B,用long转换
        cout<<"Now use cout to print m1="<<(int)m1<<endl;
        //printf %c *m1
        printf("Now use printf to print *m1=%c
    ", *m1); 
        //printf %s *m1
        // printf("Now use printf to print m1= %s
    ",*m1); 
        //printf %d m1 
        printf("Now use printf to print m1=%d
    ", m1); 
        //printf %s m1
        printf("Now use printf to print m1= %s
    ",m1); 
        /*                                       
        message[0] = m1;                    
        message[1] = m2;                    
        message[2] = m3;                    
             
        for (i=0; i<3; i++)  
        printf("%s
    ", message[i]); 
        */
    }


    输出:

    Now use cout to print *m1=c
    Now use cout to print m1=coconut is lovely
    Now use cout to print m1=4197320
    Now use printf to print *m1=c
    Now use printf to print m1=4197320
    Now use printf to print m1= coconut is lovely
    

    Ref:

    1. http://blog.csdn.net/feliciafay/article/details/6818009

     

  • 相关阅读:
    我最讨厌画图,这辈子我都不想再画图
    bzoj1218[HNOI2003]激光炸弹
    bzoj1196[HNOI2006]公路修建问题
    bzoj1588[HNOI2002]营业额统计
    bzoj2039[2009国家集训队]employ人员雇佣
    bzoj3874[Ahoi2014]宅男计划
    bzoj2282[Sdoi2011]消防
    bzoj1798[Ahoi2009]Seq 维护序列seq
    bzoj4003[JLOI2015]城池攻占
    bzoj2809[Apio2012]dispatching
  • 原文地址:https://www.cnblogs.com/space-place/p/8540209.html
Copyright © 2011-2022 走看看