您的位置:首页 > 其它

关于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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐