关于strlen/sizeof函数在char和string类型中的应用
#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;
}