字符串长度计算C++ ( 不用函数c语言sizeof和strlen/strlen!) 帮忙看一眼代码哪里出错了。

字符型数组和字符数组
sizeof和strlen的区别
cin.get(input,Arsize)
来源:博客园
strlen strlen所作的仅仅是一个计数器的工作,它从内存的某个位置(可以是字符串开头,中间某个位置,甚至是某个不确定的内存区域)开始扫描,直到碰到第一个字符串结束符'\0'为止,然后返回计数器值(长度不包含'\0')。 指实际字符串或字符数组的实际长度(不是所占空间的字节数)。 函数原型 extern unsigned int strlen(char *s) 意思是,他从指针指向的某个地址开始扫描,并读取字符,一直读到'\0'为止,不包括'\0' strlen用于字符数组或者指针 字符型数组 char B[]={‘a’,’e’,’s’,’r’,’q’}; 末位没有'\0',表示数组元素都是字符型 字符数组 char C[]={"abcdef"}; 数组元素也是字符型,但是末位有'\0' char D[]={'a','c','q','f','w'}; i=strlen(D);
//这样,由于没指定D的内存分配大小,用strlen求其长度会造成错误。 数组或字符串的长度 sizeof()---求所占的字节数 1、对于整型字符型数组 2、对于整型或字符型指针 strlen()---字符数组或字符串所占的字节数 1、针对字符数组 2、针对字符指针 sizeof()返回的是变量声明后所占的内存数,不是实际长度,此外sizeof不是函数,仅仅是一个操作符,strlen是函数。 实用模板时,需要对临时参数进行初始化,例如T temp=T(0)。 cin.get(input,Arsize)将一直读取输入,直到到达行尾或读取了ArSize-1个字符为止。 自动变量是栈,动态存储或自由存储是堆
免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动strlen(a) 用strlen计算各类型数组长度的时候是怎么来计算的?_百度知道博客访问: 2097412
博文数量: 409
博客积分: 10227
博客等级: 上将
技术积分: 8571
注册时间:
认证徽章:
非淡泊无以明志,非宁静无以致远
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: C/C++
sizeofCC++--sizeof
sizeofsizeoftype
sizeofsizeofvar_namesizeofvar_name
sizeof(var_name)sizeofvar_name
sizeofvoid
sizeof(max)maxintmax(),sizeof(char_v)char_vcharchar_v[MAX]MAXsizeof(void)
sizeofsize_ttypedefunsignedint
1) charunsignedcharsignedchar1
2) intunsignedintshortintunsignedshortlongintunsignedlongfloatdoublelongdoublesizeofANSIC2222444810
3) sizeofMicrosoftC/C++7.0near2farhuge4Unix4
5) sizeofsizeof
struct{charb;doublex;}a;
sizeofa=12sizeofchar+sizeofdouble=9
VCsizeofsizeofsizeof
A sizeof(int),sizeof(long)int162324
int a[50]; //sizeof(a)=4*50=200;
int *a=new int[50];// sizeof(a)=4; asizeof(a),324
Class Test{static double c};//sizeof(Test)=4.
Test *s;//sizeof(s)=4,s
Class test1{ };//sizeof(test1)=1;
int func(char s[5]);
cout< //sizeof(s)
sizeof(func(1234))=4//funcintsizeof(int).
sizeof2/%3i*sizeofintiint
1) sizeofI/O
void*mallocsize_tsize,
size_tfread(void*ptr,size_tsize,size_tnmemb,FILE*stream)
void*memsetvoid*s,intc,sizeof(s)
sizeof char
32324sizeof4&&
&&& 1). sizeof = *&&
&&& 2). sizeof= *)&&
&&& typedef struct student
&&& } node1;
&&& typedef struct teacher
&&& } node2;
&&& sizeof(node1)=4//sizeof
&&& sizeof(node2)=8//
VCsizeofVCsizeof
struct MyStruct
double dda1;
MyStructsizeofsizeof(MyStruct)
sizeof(MyStruct)=sizeof(double)+sizeof(char)+sizeof(int)=13
VCsizeof(MyStruct)16VC
(vc6.0,32)
sizeof(char)1
sizeof(int)4
sizeof(float)4
sizeof(double)8
sizeof(short)2
struct MyStruct
double dda1;
VCdda10sizeof(double)sizeof(double)=8dda8sizeof(char)dda8sizeof(char)=1type9sizeof(int)=4VC312sizeof(int)=4type12sizeof(int)=48+1+3+4=16sizeof(double)=8sizeof(MyStruct)=8+1+3+4=163VC
struct MyStruct
double dda1;
VC6.0sizeof(MyStruc)24VC
struct MyStruct
double dda1;//1sizeof(double)=878VC7dda188
int type//16sizeof(int)=4intVCtype164
}//1+7+8+4=20
//sizeof(double)=84sizeof(double)=8
sizeof(MyStruc)1+7+8+4+4=247+4=11VC
1extern int strlen(char *s);
// strlen.c
char *s="Golden Global View";
printf("%s has %d chars",s,strlen(s));
getchar();
c"\0"unsigned char a[15]={2,3,5,0,9,13};strlen(a)0ASCII"\0"ASCII0strlen(a)36strlen()"\0",strlen()
int strlen(const char* str)
while(*str++) i++;
& #include
& size_t strlen(const char *s);
& size_t sizeof()
& strlensizeofstrlensizeofsizeofsizeof
& 1). strlennullsizeofnull
& 2). strlenchar*'\0'sizeofstrlensizeof&&
& 3). sizeofsizeof sizeof &&
fun(char [])& fun(char& *)&
& &&C/C++&&
&& fun(const char& *s, int len)
&&&&&&& char* buf [len+1];&
&&&&&&& memcpy(buf, s, len);
阅读(2175) | 评论(0) | 转发(4) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。请大神帮忙看看这段C++代码哪儿有错误!测试时语法什么的是没有错误的。。。_百度知道字符串"\\ \22a,0\n" 的长度是?为什么看了很多种说法,\\算一个,空格算一个,\22 算一个,a算一个,逗号算一个,0算一个,\n算一个,\0算1个,这样明明是8个字节,可是这一题的答案是7,也没说是不是strlen.那到底是几啊
温存迷醉丶蘧
提问中的叙述很对.在C/C++中,说字符串长度时,指的是用strlen()测出来的长度,题中这个字符串长度自然是7;而说字符串占用的字节数或空间时,却不能忘了最后那个'\0'.
你的意思是,如果题目问的是字符串的长度的话,就是7个,如果问的是占用字节数或者空间,就是8个咯?
为您推荐:
其他类似问题
扫描下载二维码

我要回帖

更多关于 c sizeof strlen 的文章

 

随机推荐