avatar
q*0
1
See the code below. I am confused that "len = 5" from main function, "len =
4" from RemoveDuplicate function. When does terminate '\0' count? Thanks.
#include
using namespace std;
void RemoveDuplicate(char str[])
{
int len = sizeof(str)/sizeof(str[0]);
cout << "len = " << len << endl;
}
int main()
{
char a[] = "abcd";
int len = sizeof(a)/sizeof(a[0]);
cout << "len = " << len << endl;
RemoveDuplicate(a);
return 0;
}
avatar
l*a
2
please use "abcde" as your input and check the result

=

【在 q******0 的大作中提到】
: See the code below. I am confused that "len = 5" from main function, "len =
: 4" from RemoveDuplicate function. When does terminate '\0' count? Thanks.
: #include
: using namespace std;
: void RemoveDuplicate(char str[])
: {
: int len = sizeof(str)/sizeof(str[0]);
: cout << "len = " << len << endl;
: }
: int main()

avatar
i*e
3
Try this and figure out why:
void foo(int str[])
{
int len = sizeof(str)/sizeof(str[0]);
cout << "len = " << len << endl;
}
int main()
{
int a[] = {1,2,3,4};
int len = sizeof(a)/sizeof(a[0]);
cout << "len = " << len << endl;
foo(a);
return 0;
}
avatar
r*k
4
void RemoveDuplicate(char str[])
{
int len = sizeof(str)/sizeof(str[0]);
cout << "len = " << len << endl;
}
这上面这段code中,sizeof(str)是计算“the size of the pointer represented by
the array identifier”, which is always 4.
avatar
S*I
5
not necessarily always 4; size of a pointer is implementation defined.

by

【在 r*****k 的大作中提到】
: void RemoveDuplicate(char str[])
: {
: int len = sizeof(str)/sizeof(str[0]);
: cout << "len = " << len << endl;
: }
: 这上面这段code中,sizeof(str)是计算“the size of the pointer represented by
: the array identifier”, which is always 4.

avatar
r*k
6
Yes, you are right. I meant that in lz's test case, it's always 4.

【在 S**I 的大作中提到】
: not necessarily always 4; size of a pointer is implementation defined.
:
: by

avatar
a*a
7
...not

【在 r*****k 的大作中提到】
: Yes, you are right. I meant that in lz's test case, it's always 4.
avatar
p*i
8
If you are on a 64bit environment and compiles the source code in 64bit mode
, the main function will output len = 5 (length of the C string) and the
RemoveDuplicate function will output len=8, which is exactly sizeof(char*)/
sizeof(char)

=

【在 q******0 的大作中提到】
: See the code below. I am confused that "len = 5" from main function, "len =
: 4" from RemoveDuplicate function. When does terminate '\0' count? Thanks.
: #include
: using namespace std;
: void RemoveDuplicate(char str[])
: {
: int len = sizeof(str)/sizeof(str[0]);
: cout << "len = " << len << endl;
: }
: int main()

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。