阅读下面程序: include<iostream.h> void fun1(char a,char b) { char c; c=a;a=b;b=c; } void f

题目
阅读下面程序: include void fun1(char a,char b) { char c; c=a;a=b;b=c; } void f

阅读下面程序:

include<iostream.h>

void fun1(char a,char b)

{

char c;

c=a;a=b;b=c;

}

void fun2(char&a,char&b)

{

char c;

c=a;a=b;b=c;

}

void main()

{

char a,b;

a='x';b='y';

fun1(a,b);cout<<a<<b;

a='x';b='y';

fun2(a,b);cout<<a<<b;

}

则该程序的输出为______。

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

下列程序的运行结果是______。include class Base { public: void f(int x){cout<<“B

下列程序的运行结果是______。

include<iostream.h>

class Base

{

public:

void f(int x){cout<<“Base:”<<x<<endl;}

);

class Derived:public Base

{

public:

void f(char*str){cout<<“Derived:”<<str<<endl;}

};

void main(void)

{

Base*pd=ne


正确答案:Base:97。
Base:97。 解析: 本题主要考查两个知识点,一是基类指针可以指向派生类对象,并可以访问派生类的所有成员。二是在函数重载中进行隐式类型转换。如pd->f(‘a’);系统到底调用哪个重载函数呢?实参既不是派生类中的形参,也不是基类中f函数的形参类型。此时系统根据就近原则和从高优先级到低优先级的规则尝试隐式转换。单字符更接近整数,故调用的是基类的f函数。

第2题:

设有以下函数:

void fun(int n,char*$s){……}

则下面对函数指针的定义和赋值均正确的是

A.void(*pf)( );pf=fun;

B.void*Pf( );pf=fun;

C.void*pf( ); *pf=fun;

D.void(*pf)(int,char);nf=&fun;


正确答案:A
解析:函数指针的定义形式是:数据类型标识符(*指针变量名)( )。void(*pf)( )定义了一个没有返回值的函数指针pf,在给函数指针变量赋值时,只需给出函数名而不必给出参数。所以给pf赋值时,只把函数名fun赋给pf即可。所以正确答案为选项A)。

第3题:

设有以下函数

void fun(int n,char * s) { …… }

则下面对函数指针的定义和赋值均是正确的是

A)void (*pf)(); pf=fun;

B)viod *pf(); pf=fun;

C)void *pf(); *pf=fun;

D)void (*pf)(int,char);pf=&fun;


正确答案:A

第4题:

下面的程序各自独立,请问执行下面的四个TestMemory 函数各有什么样的结果?

①void GetMemory(char * p)

{

p = (char * )malloc(100);

}

void TestMemory (void)

{

char *str = NULL;

GetMemory (str);

strcpy(str, "hello world");

prinff(str);

}

② char * GetMemory (void)

{

char p[ ] = "hello world";

return p;

}

void TestMemory (void)

{

char * str = NULL;

str = GetMemory( );

printf(str);

}

③void GetMemory(char * * p, int num)

{

* p = (char * )malloc(num);

}

void TestMemory (void)

{

char * str = NULL;

GetMemory(&str, 100);

strcpy( str, "hello" );

printf(sir);

}

④void TestMemory (void)

{

char *str = (char * )malloe(100);

strepy (str, "hello" );

free ( str );

if(str ! = NULL)

{

strepy( str, "world" );

printf(str);

}

}


正确答案:程序1程序崩溃。因为GetMemory并不能传递动态内存TestMemory函数中的str一直都是 NULL。strcpy(str “hello world”);将使程序崩溃。 程序2可能是乱码。因为GetMemory返回的是指向“栈内存”的指针该指针的地址不是 NULL但其原来的内容已经被清除新内容不可知。 程序3能够输出hello但是会发生内存泄漏。 程序4篡改动态内存区的内容后果难以预料非常危险。因为free(str);之后str成为野指针if(str!=NULL)语句不起作用。
程序1程序崩溃。因为GetMemory并不能传递动态内存,TestMemory函数中的str一直都是 NULL。strcpy(str, “hello world”);将使程序崩溃。 程序2可能是乱码。因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是 NULL,但其原来的内容已经被清除,新内容不可知。 程序3能够输出hello,但是会发生内存泄漏。 程序4篡改动态内存区的内容,后果难以预料,非常危险。因为free(str);之后,str成为野指针,if(str!=NULL)语句不起作用。

第5题:

char *GetMemory(void){ char p[] = "hello world";return

p; }void Test(void){char *str = NULL;str = GetMemory(); printf(str);}请问运行 Tes

t 函数会有什么样的结果?


正确答案:
 

第6题:

设有以下函数: void fun(int n,char*s){…} 则下面对函数指针的定义和赋值均正确的是( )。

A.void(*pf)( );pf=fun;

B.void*pf( );pf=fun

C.void*pf( );*pf=fun;

D.void(*pf)(int,char);pf=&fun;


正确答案:A
函数指针的定义格式为函数类型(*指针变量名)(形参列表);函数名和数组名一样代表了函数代码的首地址,因此在赋值时,直接将函数指针指向函数名就行了。所以选项A正确。

第7题:

char*getmemory(void)

{ char p[]=”hello world”;

return p;

}

void test(void)

{

char *str=null;

str=Getmemory();

printf(str);

} 请问运行 Test 函数会有什么样的结果.


正确答案:
 

第8题:

下面程序的结果是()。includevoid main(){char *str;str="test!";cout<

下面程序的结果是( )。#include<iostream.h>void main(){char *str;str="test!";cout<<str[5];}

A.程序错误

B.!

C.'\0'

D.为空字符


正确答案:D

第9题:

设有以下函数:

voidfun(intn,char}s){……}

则下面对函数指针的定义和赋值均正确的是( )。

A.void(*pf)(int,char);pf=&fun;

B.void+pf( );pf=fun;

C.void*pf( );*pf=fun;

D.void(*pf)(int,char*);pf=fun;


正确答案:D
函数的参数可以是指针类型。它的作用是将一个变量的地址传送到另一个函数中。函数名代表函数的人口地址,指向函数的指针应该定义为void(+pf)()。如果定义为void·pf(),则表示函数pf返回值为一个基类型为void的指针。因此D选项正确。

第10题:

分析下面的程序,指出程序中的错误: # include <stdio.h> int main(void) { char a; char *str=&a; strcpy(str,"hello"); printf("%sn",str); return 0; }


342错了,字节数据最大是255