关于strlen/sizeof函数在char和string类型中的应用
2008-02-01 10:01
495 查看
#include<iostream>
using namespace std;
int main()
{
// typedef struct student
//{
// char name[10];
// char sex;
// long sno;
// float score [4];
//} STU;
//
//STU a[5];
//
//cout<<sizeof(a)<<endl;
//
//return 0;
char ghost[15] = "galloping";
char * str = "galloping";
int n1 = strlen(ghost); //字符数组中字符的实际长度
int n2 = strlen(str); //指针指向的字符数组的实际长度
int n3 = strlen("galloping"); //字符串中字符的实际长度
int n4 = sizeof(ghost); //字符数组分配空间大小
int n5 = sizeof(str); //指针分配的空间大小
int n6 = sizeof("galloping"); //字符串分配空间大小,注意最后一位要加上'\0'
cout <<"n1 = "<<n1<<endl;
cout <<"n2 = "<<n2<<endl;
cout <<"n3 = "<<n3<<endl;
cout <<"n4 = "<<n4<<endl;
cout <<"n5 = "<<n5<<endl;
cout <<"n6 = "<<n6<<endl;
}
using namespace std;
int main()
{
// typedef struct student
//{
// char name[10];
// char sex;
// long sno;
// float score [4];
//} STU;
//
//STU a[5];
//
//cout<<sizeof(a)<<endl;
//
//return 0;
char ghost[15] = "galloping";
char * str = "galloping";
int n1 = strlen(ghost); //字符数组中字符的实际长度
int n2 = strlen(str); //指针指向的字符数组的实际长度
int n3 = strlen("galloping"); //字符串中字符的实际长度
int n4 = sizeof(ghost); //字符数组分配空间大小
int n5 = sizeof(str); //指针分配的空间大小
int n6 = sizeof("galloping"); //字符串分配空间大小,注意最后一位要加上'\0'
cout <<"n1 = "<<n1<<endl;
cout <<"n2 = "<<n2<<endl;
cout <<"n3 = "<<n3<<endl;
cout <<"n4 = "<<n4<<endl;
cout <<"n5 = "<<n5<<endl;
cout <<"n6 = "<<n6<<endl;
}
相关文章推荐
- Java String,int,Integer,char 类型转换 ,sizeof,strlen,length的区别 .
- string,char,char*,char a[], sizeof,strlen(),str.length()...
- 【1】ptr = malloc(0*sizeof(char))【2】关于typedef的用法总结【3】关于有符号数、无符号数和数据类型的总结
- string,char,char*,char a[] 占字节数, 以及sizeof,strlen(),str.length()的用法
- C语言中strlen()函数和sizeof()函数区别[关于字符串长度]
- 重写string.h中的字符串操作函数--strlen,strlen 与sizeof的区别
- sizeof strlen string.lenth()之例子 string,char,char*,char a[], sizeof,strlen(),str.length()...
- char字符串数组与string之间的转换,以及sizeof,strlen,length的区别
- 数据类型与strlen()函数sizeof()运算符
- 关于sizeof运算符和strlen()函数的个别区别分析
- string,char,char*,char a[] 占字节数, 以及sizeof,strlen(),str.length()的用法
- [c语言]sizeof() 居然不是函数?!!strlen()和sizeof()纠缠在一起再碰到不同类型的数组,你还能分辨清吗?
- 关于sizeof与strlen在数组中的应用
- 【1】ptr = malloc(0*sizeof(char))【2】关于typedef的用法总结【3】关于有符号数、无符号数和数据类型的总结 .
- 关于C和C++中的基本数据类型int、long、long long、float、double、char、string的大小及表示范围
- strlen函数与sizeof函数的区别及应用
- 关于C和C++中的基本数据类型int、long、long long、float、double、char、string的大小及表示范围
- C/C++关于字符串string、char*字符串数组与其他类型转换
- 关于sizeof与strlen在字符串应用的区别
- 关于sizeof/strlen、char */char []