有以下程序: #include <iostream> #include <string> usxng namespace std; int main() { char p[] = "abcdefgh";

admin2010-03-29  12

问题 有以下程序:    #include  <iostream>    #include  <string>    usxng namespace std;    int main()    {       char p[] = "abcdefgh";       cout<<strlen(strcpy(p,"12345"))<<end1;       return 0;    }    执行后输出的结果是(    )。

选项 A、8
B、12
C、5
D、7

答案4

解析 本题考查对字符串函数的熟悉程度。本题主要考查strlen和strcpy两个函数,先来了解这两个函数。
   ①函数strcpy()的函数原型为:char*strcpy(char*strDest,const char*strSrC) ;其功能是复制strSrc所有字符到strDest,并返回strDest。
   ②函数strlen()的函数原型为:size_t strlen(const char *string);,其功能是返回string的长度,不包括结束字符’\0’。
   在了解函数的原型和功能后,再分析本程序。程序首先定义了字符数组p,并赋初值"abcdefg",然后将字符串"12345"复制到数组p中,此时数组中元素变为字符串 "12345",然后调用函数strlen求出数组p中的字符数为5(不包括结束标志符’\0’)。
转载请注明原文地址:https://kaotiyun.com/show/V6jp777K
0

最新回复(0)