第八章指针实验

第八次实验报告
实验项目:指针实验
姓名:廖云福 实验地点: 514物联网实验室 实验时间:2019年6月12日

实验项目
指针基础及指针运算
数据交换
字符串反转及字符串连接
数组元素奇偶排列
一、实验目的和要求
(1)掌握指针的概念和定义方法。
(2)掌握指针的操作和运算
(3)掌握指针与数组的关系
(4)掌握指针与字符串的关系
(5)熟悉指针作为函数的参数以及返回指针函数
(6)了解函数指针
二、实验内容
实验一、 8.3.1指针基础及指针运算
1、问题描述
定义一个整型指针变量p,使'yi它指向一个整型变量a,定义一个浮点型指针q,使它指向一个浮点型变量b,同时定义另外一个整型变量c并赋值初值3 。使用指针变量,调用scanf函数分别输入a和b的值。通过指针间接访问并输出a、b的值。按16进制方式输出pq的值以及a、b的地址。将p指向c,通过p间接访问c的值并输出。输出p的值以及c的地址,并与上面的结果进行比较。
2、实验代码

#include<stdio.h>
int main()
{
    int *p,a,c=3;
    float *q,b;
    p=&a;
    q=&b;
    printf("pleasse input the value of a,b:");
    scanf("%d%f",&a,&b);
    printf("result:
");
    printf("      %d,%f
",a,b);
    printf("      %d,%f
",*p,*q);
    printf("the addrese of a,b:%p,%p
",&a,&b);
    printf("the addrese of a,b:%p,%p
",p,q);
    p=&c;
    printf("c=%d
",*p);
    printf("the addrese of a,b:%x,%x
",p,&c);
    return 0;
      
}

  

运行结果

3、问题分析:这个程序还是比较简单的,问题不大。

实验二、8.3.2数据交换
1、问题描述
定义俩个函数,分别是swap1和swap2,用于交换a,b的值。

2、实验代码

#include<stdio.h>
void swap1(int x,int y);
void swap2(int *x,int *y);
int main()
{
	int a,b;
	printf("please Input
 a=:");
	scanf("%d",&a);
	printf("
b=:");
	scanf("%d",&b);
	swap1(a, b);
	printf("
After Call swap1:a=%d b=%d
",a,b);
	swap2(&a,&b);
	printf("
After Call swap2:a=%d b=%d
",a,b);
	return 0;
}
void swap1(int x,int y)
{
	int temp;
	temp=x;
	x=y;
	y=temp;
}
void swap2(int *x,int *y)
{
	int temp;
	temp=*x;
	*x=*y;
	*y=temp;
}

运行结果

3、问题分析:实参的传递就是要把a,b的地址传递过去,所以是&a,&b,交换x,y地址上的值需要用到指针,x,y,其他的就没有什么问题了

实验三、8.3.3字符串反转字符串连接
1、问题描述
定义俩个字符指针,通过get()函数输入俩个字符串。定义一个函数charreverse(charstr),通过指针移动的方式将字符串反转。再定义一个函数charlink(charstr1,char*str2),通过指针移动方式将俩个字符串连接起来。从主函数中分别调用上述函数,输入字符marrsort(int a[],int n);

2、实验代码

#include<stdio.h>
char *reverse(char *str);
char *link(char *str1,char *str2);
int main()
{
	char str[30],str1[30],*str2;
	printf("Input Reversing Character String:");
	gets(str);
	str2=reverse(str);
	printf("
Output Reversed Character String:");
	puts(str2);
	printf("Input String1:");
	gets(str);
	printf("
Input String2:");
	gets(str1);
	str2=link(str,str1);
	puts(str2);
	return 0;
}
char *reverse(char *str)
{
	char *p,*q,temp;
	p=str,q=str;
	while(*p!='')
	p++;
	p--;
	while(q<p)
	{
		temp=*q;
		*q=*p;
		*p=temp;
		q++;
		p--;
	}
	return str;
}
char *link(char *str1,char *str2)
{
	char *p=str1,*q=str2;
	while(*p!='')
	p++;
	while(*q!='')
	{
		*p=*q;
		q++;
		p++;
    }  
    *q='';
	return str1;
}

运行结果

3、问题分析:指针的相向移动就是使两个指针向靠近,p--,q++,判断指针是否到达最后一个字符,只需*p!=即可

实验四、 8.3.4数组元素奇偶排列
1、问题描述
定义一个整型一维数组,任意输入数组的元素其中包含奇数和偶数,定义一个函数,实现将数组元素奇数在左、偶数在右的排列。在上述定义的函数中,不允许再增加新的数组,从主函数中分别调用上述函数,打印输出结果。


#include<stdio.h>
#define N 10
void arrsort(int a[],int n);
int main()
{
	int a[N],i;
	for(i=0;i<N;i++)
	scanf("%d",&a[i]);
	arrsort(a,N);
	for(i=0;i<N;i++)
	printf("%2d", a[i]);
}
void arrsort(int a[],int n)
{
	int *p,*q,temp;
	p=a;
	q=a+n-1;
	while(p<q)
	{
		while(*p%2!=0)
		p++;
		while(*q%2==0)
		q--;
		
		if(p>q)
		break;
		temp=*p;
		*p=*q;
		*q=temp;
		p++;
		q--;
	}
}

运行结果

3、问题分析:这个程序没啥问题,但是要理解这个程序的算法。

三、实验小结
虽然这次的实验是关于指针部分的,但这次实验的难度并不大,或许这只是指针的一点皮毛罢了,这一部分要好好学,只有学好了指针才能将c语言运用的随心顺手。

原文地址:https://www.cnblogs.com/lyf152977/p/11025283.html