修改后的第二次作业

#include<stdio.h>
main()
{
	void swap(int *p1,int *p2);//定义空类型交换两个变量的函数// 
	int a,b;
	int *h1,*h2;
	printf("please enter two integer numbers:");
	scanf("%d,%d",&a,&b);//输入两个变量// 
	h1=&a;
	h2=&b;
	swap(h1,h2);//交换 h1,h2// 
	printf("%d,%d",*h1,*h2);
	return 0;	
}
void swap(int *p1,int *p2)//调用子函数,交换两个数的值// 
{
	int p;
	p=*p1;
	*p1=*p2;
	*p2=p;
}
please enter two integer numbers:12,20
20,12
--------------------------------
Process exited after 10.89 seconds with return value 0
请按任意键继续. . .

题目一:

总结:1.刚开始的时候忘记在主函数中加入swap(h1,h2)了,导致直接输出两个数值。

        2.写子函数的时候参考书上的例题8.4,输出的结果仍然没有交换数值,经过调整成功的交换了,但是仍有疑问。

        3.对知识点还是没有充分理解和应用。

题目二:

参考书中的解释:未给*m赋值,因此m中无确定的值,所以m所指向的单元是不可预见的。所以,对*m赋值就是向一个未知的存储单元赋值,而这个存储单元中可能存储着一个有用的数据,这样就有可能破坏系统的正常工作状况。

题目三:

c语言中实参变量和形参变量之间的数据传递是单向的“值传递”方式。用指针变量作函数参数时同样要遵循这一规则。

题目四:

#include<stdio.h>
int main()
{
    int comp(int,int);//函数声明 
    int (*p)(int,int);//定义指向函数的指针变量p 
	int a,b,c;
	p=comp; //使p指向comp函数 
    printf("please enter a and b:");
    scanf("%d,%d",&a,&b);
    c=(*p)(a,b);//调用comp函数 
    printf("a=%d\nb=%d\nc=%d\n",a,b,c);
    return 0; 
}
int comp(int x,int y)//定义comp函数 
{
	int z;
    if(x>y)   z=x;
    else   z=y;
    return(z);
}
please enter a and b:12,25
a=12
b=25
c=25

--------------------------------
Process exited after 8.51 seconds with return value 0
请按任意键继续. . .

总结:参考书中的例题,我发现我在定义指向函数的指针变量p时不会定义,声明函数时也不知道如何声明,现在会了。

posted @ 2017-03-26 11:08  圣星  阅读(191)  评论(0编辑  收藏  举报