字符串指针与char型指针数组
来源:互联网 发布:肌研极润怎么样知乎 编辑:程序博客网 时间:2024/06/01 14:42
一、字符串指针
字符串是一种特殊的char型数组,指向char类型数组的指针,就是字符串指针。与普通指针一样,字符串指针在使用前也必须定义。字符串与char数组的区别在于长度,字符会自动在尾部加上一个长度‘\0’,而char型数组的长度就是其字符的个数。字符串长度是字符个数+1。例:
- #include<iostream>
- using namespace std;
- int main()
- {
- char str[]="hello world";
- char *p=str;
- cout<<str<<endl;
- cout<<p<<endl;
- cout<<*p<<endl;
- system("pause");
- }
二、char型指针数组
指针数组的元素是指针。与普通指针类似,指针数组在使用前也必须先赋值,否则可能指向没有意义的值,这是比较危险的。以char型指针数组为例。
- int main()
- {
- char *p[5]={"abc","def","ghi","jkl","mno"};
- for(int i=0;i<5;i++)
- puts(p[i]);
- system("pause");
- }
对于int型数据,如下:
- #include<iostream>
- using namespace std;
- int main()
- {
- int a=1,b=2,c=3;
- int *p[]={&a,&b,&c};//不能写成int *p[]={1,2,3},这是不合法的。因为p与[]先结合,是一个指针数组,数组的数据元素是int型指针,而不是int型数据。
- for(int i=0;i<3;i++)
- cout<<p[i]<<endl;//打印出地址(指针)
- system("pause");
- }
三、对比int型和char型数组的数组名和取地址
- #include<iostream>
- using namespace std;
- int main()
- {
- int a[]={1,2,3};
- char b[]={'a','b','c'};
- cout<<a<<endl;
- cout<<&a<<endl;
- cout<<b<<endl;
- cout<<&b<<endl;
- system("pause");
- }
0 0
- 字符串指针与char型指针数组
- 字符串指针与char型指针数组
- 字符串与char数组的指针表示
- char* ,char[],指针与数组
- char数组与char指针
- char数组与char指针
- char数组 、char指针与字符串常量的比较
- char数组 、char指针与字符串常量的比较
- 二维char数组与二维char指针
- char *指针与char 数组的区别
- 字符串数组与指针
- 字符串数组与指针
- 数组、指针与字符串
- 数组、指针与字符串
- 数组、字符串与指针
- 字符串数组与指针
- 数组字符串与指针字符串的区别 char s[]="***" 和char *s="***"的区别
- char型指针数组与字符数组的区别
- java:用UDP协议,开发一个聊天工具代码
- HDU 1598 ( find the most comfortable road )
- 01-复杂度1. 最大子列和问题(20)
- Eclipse里添加Tomcat服务器
- leetcode[224]:Basic Calculator
- 字符串指针与char型指针数组
- KNN算法学习--python实现和java实现
- 设计模式之-----建造者模式
- 曾国藩:成大事者不纠结
- C++类中静态变量和静态方法的注意事项
- AfxGetApp()解析
- poj3267 The Cow Lexicon -DP
- Struts2-02
- zoj博弈月赛