指向字符串的指针在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)
函数指针: 指向函数的指针, 首先是一个指针, 这个指针指向一个函数. 函数具有可赋值给指针的物理内存地址,一个函数的函数名就是一个指针,它指向函数的代码.一个函数的地址是该函数的进入点,也是调用函数 ...
随机推荐
- zipkin:和springcloud集成过程记录
发现全是springcloudapp的名称,然后是springcloudapp(http://localhost:8080/hello/tom)工程单独调用并没有通知zipkin: 原来是因为rest ...
- 3.Appium运行时出现:Original error: Android devices must be of API level 17 or higher. Please change your device to Selendroid or upgrade Android on your device
参考博客:https://blog.csdn.net/niubitianping/article/details/52624417 1.错误信息:Original error: Android dev ...
- 转-在Mac OS上搭建Python的开发环境
在Mac OS上搭建Python的开发环境 本文转载自:http://www.jb51.net/article/76931.htm 一. 安装python mac系统其实自带了一个python的执 ...
- Oracle SYS_CONTEXT用法
1. USERENV(OPTION) 返回当前的会话信息. OPTION='ISDBA'若当前是DBA角色,则为TRUE,否则FALSE. OPTION='LANGUAGE'返回数据库的字符集. OP ...
- 老叶观点:MySQL开发规范之我见(更新版)
转自:http://mp.weixin.qq.com/s?__biz=MjM5NzAzMTY4NQ==&mid=207239419&idx=2&sn=bddbe0a657758 ...
- linux编程基本
库的使用头文件:.h 里面的函数及变量的声明 比如#include <stdio.h> ,Linux下默认头文件的搜索路径 系统定义的头文件: /usr/include /usr/loca ...
- find 和 find_all 用法
soup = BeautifulSoup(requests.get(url).text, 'html.parser') soup.find('span', class_='item_hot_topic ...
- Java 中的包装类
Java 中的包装类 相信各位小伙伴们对基本数据类型都非常熟悉,例如 int.float.double.boolean.char 等.基本数据类型是不具备对象的特性的,比如基本类型不能调用方法.功能简 ...
- 管理Linux服务器的用户和组
管理Linux服务器的用户和组 Linux操作系统是一个多用户多任务的操作系统,允许多个用户同时登录到系统,使用系统资源. 为了使所有用户的工作顺利进行,保护每个用户的文件和进程,规范每个用户的权限, ...
- PyQt5系列教程(五)制作fastboot烧写器
软硬件环境 Windows 7 Python 3.4.2 PyQt 5.5.1 PyCharm 5.0.2 前言 fastboot是针对Android设备的一种刷机方式,它比recovery更底层,刷 ...