#include "iostream"
//#include <string>
#include <cstring>//strlen
/**<string>是C++标准库头文件,包含了拟容器class std::string的声明(不过class string事实上只是basic_string<char>的typedef),用于字符串操作。
*cstring>是C标准库头文件<string.h>的C++标准库版本,包含了C风格字符串(NUL即'\0'结尾字符串)相关的一些类型和函数的声明,例如strcmp、strchr、strstr等。<cstring>和<string.h>的最大区别在于,其中声明的名称都是位于std命名空间中的,而不是后者的全局命名空间。
*/
using namespace std;
//字符串数组首地址作为参数时,数组大小不能一同传入
int printnum(char** elem){
	if(elem = NULL)cout<<"elem = NULL"<<endl;
	cout<<"sizeof(elem) = "<<sizeof(elem)<<endl;//指针都是4个字节
	cout<<"sizeof(elem[0]) = "<<sizeof(elem[0])<<endl;
	int x = sizeof(elem)/sizeof(elem[0]);
	return x;
}

int printstringnum(string* elem){
	if(elem = NULL)cout<<"elem = NULL"<<endl;
	cout<<"sizeof(elem) = "<<sizeof(elem)<<endl;//指针都是4个字节
	cout<<"sizeof(elem[0]) = "<<sizeof(elem[0])<<endl;
	int x = sizeof(elem)/sizeof(elem[0]);
	return x;
}

int main(){
	char* color[] = {"redyellowaddfdfg","green","blue","yellow"};
	cout<<"sizeof(color) = "<<sizeof(color)<<endl;//4*4. 求的不是元素数组大小,而是元素的所有指针占据的大小
	cout<<"sizeof(color[0]) = "<<sizeof(color[0])<<endl;//4.求的不是元素大小,而是指针大小
	cout<<"sizeof(char*) = "<<sizeof(char*)<<endl;//4
	cout<<"sizeof(color[3]) = "<<sizeof(color[3])<<endl;//4
	cout<<"strlen(color[0]) = "<<strlen(color[0])<<endl;//16
	int y = sizeof(color)/sizeof(color[0]);
	cout<<"y = "<<y<<endl;
	int x = printnum(color);
	cout<<"x = "<<x<<endl;
	
	string col[] = {"red","green","blue","yellow"};
	cout<<"sizeof(col) = "<<sizeof(col)<<endl;//4*16,
	cout<<"sizeof(col[0]) = "<<sizeof(col[0])<<endl;//
	cout<<"sizeof(string) = "<<sizeof(string)<<endl;//
	cout<<"sizeof(col[3]) = "<<sizeof(col[3])<<endl;//
	cout<<"col[0].length = "<<col[0].length()<<endl;//
	cout<<"col[3].length = "<<col[3].length()<<endl;
	
	int y2 = sizeof(col)/sizeof(col[0]);
	cout<<"y2 = "<<y2<<endl;
	int x2 = printstringnum(col);
	cout<<"x2 = "<<x2<<endl;
	
	string str = "add";
	cout<<"str.length() = "<<str.length()<<endl;
	return 0;
}


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐