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

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\n", *m1:

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

输出:

Now use printf to print *m1=c

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

用printf 打印 %d m1

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

输出:

Now use printf to print m1=4197112

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

用printf打印%s m1:

printf("Now use printf to print m1= %s\n",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[];
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\n", *m1);
//printf %s *m1
// printf("Now use printf to print m1= %s\n",*m1);
//printf %d m1
printf("Now use printf to print m1=%d\n", m1);
//printf %s m1
printf("Now use printf to print m1= %s\n",m1);
/*
message[0] = m1;
message[1] = m2;
message[2] = m3; for (i=0; i<3; i++)
printf("%s\n", 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

指向字符串的指针在printf与cout区别的更多相关文章

  1. 指向字符串的指针和char类型的数组

    指针数组的效率比二维字符数组的效率高 指针数组不能修改字符串字面量,而二维字符数组中的内容可以更改

  2. C++(二十四) — 指向字符的指针为什么可以用字符串来初始化,而不是字符地址?

    一.C语言中,为什么字符串可以赋值给字符指针变量? char *p: a='; p=&a; //显然是正确的, p="abcd"; //但为什么也可以这样赋值?? 问:一直 ...

  3. 【转】const int *p和int * const p的区别(常量指针与指向常量的指针)

    [转]作者:xwdreamer   出处:http://www.cnblogs.com/xwdreamer 对于指针和常量,有以下三种形式都是正确的: const char * myPtr = &am ...

  4. 12-返回指针的函数&&指向函数的指针

    前言 接下来我只讲指针的最常见用法,比如这一章的内容----返回指针的函数 与 指向函数的指针   一.返回指针的函数 指针也是C语言中的一种数据类型,因此一个函数的返回值肯定可以是指针类型的. 返回 ...

  5. C语言之数组,字符串,指针

    一. 数组的定义 1.  数组初始化 初始化方式 int a[3] = {10, 9, 6}; int a[3] = {10,9}; int a[] = {11, 7, 6}; int a[4] = ...

  6. 【C语言】14-返回指针的函数与指向函数的指针

    前言 前面我们花了接近3个章节学习指针,应该都感受到指针的强大了吧.指针可以根据地址直接操作内存中的数据,使用得当的话,不仅能使代码量变少,还能优化内存管理.提升程序性能.关于指针的内容还非常多,比如 ...

  7. 指向函数的指针 分类: C/C++ 2015-07-13 11:03 14人阅读 评论(0) 收藏

    原文网址:http://www.cnblogs.com/zxl2431/archive/2011/03/25/1995285.html 讲的很清楚,备份记录. (一) 用函数指针变量调用函数 可以用指 ...

  8. 「C」 数组、字符串、指针

    一.数组 (一)数组 概念:用来存储一组数据的构造数据类型 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. (二)数组的定义 格式: 类型 数组名[元素个数 ...

  9. 指向函数的指针 ------ 函数指针(function pointer)

    函数指针: 指向函数的指针, 首先是一个指针, 这个指针指向一个函数. 函数具有可赋值给指针的物理内存地址,一个函数的函数名就是一个指针,它指向函数的代码.一个函数的地址是该函数的进入点,也是调用函数 ...

随机推荐

  1. k8s PersistentVolume hostpath 简单使用

    kubernets host PersistentVolume 测试 因为yaml 格式的问题 ,我修改为了json 创建 pv pv.json { "kind": "P ...

  2. pthread线程私有数据

    进程内所有的线程共享地址空间,任何声明为静态或外部的变量,或在进程堆声明的变量都可以被进程内的所有线程读写. static,extern,或堆变量的值是上次线程改写的值 一个线程真正拥有的唯一私有存储 ...

  3. Pgsql和Mysql的对比

    工作中用过这两个数据库,但都不是太深入,仅限于用而已,但给我留下的印象就是Pgsql更好些,因为这两个库我都遇到过数据丢失的问题,前者我通过网上方法加自己的判断有惊无险的恢复了,而后者搜索各种资料加问 ...

  4. 【转】Linux shell的&&和||

    原文网址:http://www.2cto.com/os/201302/189655.html Linux shell的&&和||   shell 在执行某个命令的时候,会返回一个返回值 ...

  5. bzoj 3328 PYXFIB——单位根反演

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3328 单位根反演主要就是有 \( [k|n] = \frac{1}{k}\sum\limit ...

  6. 微软正式发布Windows 1.0 回顾历代Windows版本界面

    在刚过去的上月底,Windows XP过完了12岁生日,在今天我们又欢快地迎来了Windows的生日.在1985年11月20日,微软正式发布Windows 1.0,它基于的是MS-DOS系统,实际上其 ...

  7. 查看ms SQL Server存储过程,函数的内容

    方法1:最简单的,右键单击要查看的存储过程,选择“修改”: 方法2: SELECT definition FROM solar.sys.sql_modules WHERE [object_id]=(O ...

  8. Java里的集合--主要区别

    Collection 集合接口,指的是 java.util.Collection接口,是 Set.List 和 Queue 接口的超类接口. List: List是关注事物索引的列表. List中可以 ...

  9. Python格式化输出%s和%d

    python print格式化输出. 1. 打印字符串 print ("His name is %s"%("Aviad")) 效果: 2.打印整数 print ...

  10. KMP算法解释

    给定两个字符串A,B,判断T是否为S的子串(变式:寻找子串B在串A中的位置). 要求一个O(|A|+|B|)的做法. 通常称A为目标串(或主串),B为模式串. 算法过程: 我们假设串A的长度为n,串B ...