Redian新闻
>
a MS interview question about C++
avatar
a MS interview question about C++# JobHunting - 待字闺中
l*y
1
how to copy a string without using strcpy function in only one clause? for
example
char * str1 = "welcome";//this is source to be copied
//want to copy str1 to dest using only one clause!
......char * dest ......//how to write this clause?
avatar
c*0
2
while( *a++ = *b++) 可以吗
这种问题啥意义啊
avatar
c*1
3
while((dest[i++] = src[i++])!= '\0');
Is that OK?
avatar
l*y
4
i think this is ok but how do you allocate memory for dest * ?
i know in some compiler it will work like mine, but still dest * is not
allocated any memory.

【在 c********1 的大作中提到】
: while((dest[i++] = src[i++])!= '\0');
: Is that OK?

avatar
l*y
5
no yiyi, only to screen ppl and find the geekest geek.
and how do u pronounce geek? [geik] or [d3ik] ?

【在 c********0 的大作中提到】
: while( *a++ = *b++) 可以吗
: 这种问题啥意义啊

avatar
l*g
6
strcpy不行,strncpy,memcpy行不行?

【在 l********y 的大作中提到】
: how to copy a string without using strcpy function in only one clause? for
: example
: char * str1 = "welcome";//this is source to be copied
: //want to copy str1 to dest using only one clause!
: ......char * dest ......//how to write this clause?

avatar
e*e
7
你应该这么问,
只许用一句instruction....听好喽。。。系instruction 哦。。。。C code, java 啥
的不算~~~
前面那个coconut001滴答案,compile出来好多好多instruction,大大的不行。
重新设计一个CPU,专门定义一个instruction,就干这个
avatar
r*g
8
是string还是character array呢?如果character array很难吧,分配空间至少算一句
了。。。

【在 l********y 的大作中提到】
: how to copy a string without using strcpy function in only one clause? for
: example
: char * str1 = "welcome";//this is source to be copied
: //want to copy str1 to dest using only one clause!
: ......char * dest ......//how to write this clause?

avatar
n*r
9
It's not pretty, but works :)
char* dest = const_cast( (new string(str1))->c_str() );

for

【在 l********y 的大作中提到】
: how to copy a string without using strcpy function in only one clause? for
: example
: char * str1 = "welcome";//this is source to be copied
: //want to copy str1 to dest using only one clause!
: ......char * dest ......//how to write this clause?

avatar
l*y
10
这个好,很难想出来啊

【在 n*******r 的大作中提到】
: It's not pretty, but works :)
: char* dest = const_cast( (new string(str1))->c_str() );
:
: for

avatar
c*t
11
但是这个没有内存泄漏问题吗?
下面这么用似乎跟安全,不过没法用一行来写
auto_ptr src(new string(str));
char * dest = const_cast(src->c_str());

【在 n*******r 的大作中提到】
: It's not pretty, but works :)
: char* dest = const_cast( (new string(str1))->c_str() );
:
: for

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