指向字符串的指针在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\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:
- http://blog.csdn.net/feliciafay/article/details/6818009
指向字符串的指针在printf与cout区别的更多相关文章
- 指向字符串的指针和char类型的数组
指针数组的效率比二维字符数组的效率高 指针数组不能修改字符串字面量,而二维字符数组中的内容可以更改
- C++(二十四) — 指向字符的指针为什么可以用字符串来初始化,而不是字符地址?
一.C语言中,为什么字符串可以赋值给字符指针变量? char *p: a='; p=&a; //显然是正确的, p="abcd"; //但为什么也可以这样赋值?? 问:一直 ...
- 【转】const int *p和int * const p的区别(常量指针与指向常量的指针)
[转]作者:xwdreamer 出处:http://www.cnblogs.com/xwdreamer 对于指针和常量,有以下三种形式都是正确的: const char * myPtr = &am ...
- 12-返回指针的函数&&指向函数的指针
前言 接下来我只讲指针的最常见用法,比如这一章的内容----返回指针的函数 与 指向函数的指针 一.返回指针的函数 指针也是C语言中的一种数据类型,因此一个函数的返回值肯定可以是指针类型的. 返回 ...
- C语言之数组,字符串,指针
一. 数组的定义 1. 数组初始化 初始化方式 int a[3] = {10, 9, 6}; int a[3] = {10,9}; int a[] = {11, 7, 6}; int a[4] = ...
- 【C语言】14-返回指针的函数与指向函数的指针
前言 前面我们花了接近3个章节学习指针,应该都感受到指针的强大了吧.指针可以根据地址直接操作内存中的数据,使用得当的话,不仅能使代码量变少,还能优化内存管理.提升程序性能.关于指针的内容还非常多,比如 ...
- 指向函数的指针 分类: C/C++ 2015-07-13 11:03 14人阅读 评论(0) 收藏
原文网址:http://www.cnblogs.com/zxl2431/archive/2011/03/25/1995285.html 讲的很清楚,备份记录. (一) 用函数指针变量调用函数 可以用指 ...
- 「C」 数组、字符串、指针
一.数组 (一)数组 概念:用来存储一组数据的构造数据类型 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. (二)数组的定义 格式: 类型 数组名[元素个数 ...
- 指向函数的指针 ------ 函数指针(function pointer)
函数指针: 指向函数的指针, 首先是一个指针, 这个指针指向一个函数. 函数具有可赋值给指针的物理内存地址,一个函数的函数名就是一个指针,它指向函数的代码.一个函数的地址是该函数的进入点,也是调用函数 ...
随机推荐
- FastAdmin 的前端环境怎么安装?
FastAdmin 的前端环境怎么安装? 安装 Git 安装 Node.js 安装 cnpm 安装 bower 开始安装 FastAdmin 的前端组件 bower install bower upd ...
- hadoop YARN配置参数剖析—MapReduce相关参数
MapReduce相关配置参数分为两部分,分别是JobHistory Server和应用程序参数,Job History可运行在一个独立节点上,而应用程序参数则可存放在mapred-site.xml中 ...
- oracle数据库启动时出现ORA-01157和ORA-01110问题
sql>startup mount; sql>alter database open; RA-01157: 无法标识/锁定数据文件 10 - 请参阅 DBWR 跟踪文件ORA-01110: ...
- SpringIoc 和 工厂模式(反射实现)
一.先演示 “简单工厂”: package org; interface Fruit { public void eat(); } class Apple implements Fruit { pub ...
- hBuilder培训资源视频教程汇总
DCloud对开发者的学习支持分3个层面:官方文档.三方专业培训.网友经验分享 DCloud的精力主要在做产品,配套的文档也会一直完善好.但专业的培训还不是DCloud能做好的,在HTML5中国产业联 ...
- 【记录】MVC4中使用SignalR
前言 周末在偶尔翻阅微软官网的时候看到Getting Started with SignalR and MVC 4此篇文章,知道了signalr这个东西,貌似这个出来很长时间了,奈何自己一直没有发现, ...
- ESN
1.对于一般的硬件设备,ESN是设备序列号,主要用来识别设备,包括未来服务鉴权的需要 2.对于需要license的设备,ESN也是设备序列号的意思,只不过这个序列号可能是根据设备硬件信息算出来的一串字 ...
- 01:zabbix监控redis
一.zabbix 自动发现并监控redis多实例 1.1 编写脚本 1.1.1 redis_low_discovery.sh 用于发现redis多实例 [root@redis02 homed]# ca ...
- socket_简单报头
client--------------------- #!/usr/bin/python3# -*- coding: utf-8 -*-# @Time : 2018/6/6 14:53# @F ...
- C++ - 常用的标准库函数
写在前面 C++是一门博大精深的语言,也是最难学的一门编程语言,每一位励志学好C++的程序员都需要从基本功开始,稳扎稳打. 自从1998年C++ standard定案以后,C++程序库便有了大幅扩 ...