c语言程序设置实验6,《C语言程序设》实验报告 实验六.doc

《C语言程序设》实验报告 实验六

《C语言程序设计》实验报告

实验六 使用指针的程序设计

学号 姓名

一、实验目的掌握指针的概念

能正确使用数组的指针和指向数组的指针变量;

熟悉指针作为函数参数的定义和调用方法;

能正确使用字符串的指针和指向字符串的指针变量。

二、实验内容#include

void main()

{

int *p1,*p2,*p;

int a,b;

printf("Input a b please");

scanf("%d%d",&a,&b);

p1=&a;

p2=&b;

if(a>b)

{p=p1;p1=p2;p2=p;}

printf("*p1=%d, *p2=%d\n",*p1,*p2);

printf("a=%d, b=%d\n",a,b);

}

『运行结果:』

输入1,2得:*p1=1,*p2=2

a=1,b=2

输入2,1得:*p1=1,*p2=2

a=2,b=1

输入56,123得:*p1=56,*p2=123

a=56,b=123

下列程序的功能是分别求出数组中所有奇数之和以及所有偶数之和。形参n给了数组中数据的个数,利用指针odd返回奇数之和,利用指针even返回偶数之和。请在下面空白处将实现这一功能的函数完善,并且调试运行出结果。

#include

#define N 10

void fun(int *a,int n,int *odd,int *even)

{

int m;

*odd=0; *even=0;

for(m=0;m

if(*(a+m)%2==0)

*even+=*(a+m);

else

*odd+=*(a+m);

}

void main()

{

int a[N]={1,10,2,3,19,6},i,n=6,odd,even;

printf("The original data is:\n");

for(i=0;i

printf("%5d",*(a+i));

printf("\n\n");

fun(a,n,&odd,&even);

printf("The sum of odd numbers:%d\n",odd);

printf("The sum of even number:%d\n",even);

}

『运行结果:』

编程实现从键盘输入一个字符ch和一个字符串str,利用字符指针实现删除字符串str中和字符ch相等所有字符,然后输出字符串str。运行程序并写出2组以上程序运行结果。

程序代码:

#include

#include

char *del(char str,char m);

main()

{

char *str,*p,m;

printf("输入一段字符串");

gets(str);

printf("输入一个想删除的字符");

m=getchar();

p=del(str,m);

printf("%s\n",p);

}

char *del(char *str,char m)

{ int h=0;

char *s;

s=str;

int n=strlen(str);

for( ;h<=n;h++)

{if(*(str+h)!=m)

{

*s=*(str+h);

s++;

}

}

*s='\0';

}

『运行结果:』

三、问题讨论

1. 简述字符串数组和字符指针的区别,举例说明。

字符串数组是用来存放字符串的数组,再内存中占有一段连续的单元。字符指针是指向字符的指针,所占内存单元存放的是所指字符的内存单元。

The rectification measures: (LED Leadership: Luo Mingjun, rectification time: before September 25th, 1, long-term adherence) to solve the problem of lack of awareness of the harm of Party cadres the "four winds". To further strengthen education and guidance on the implementation of the central eight provisions, the provincial nine provisions of the county committee of the "ten provisions" extensive publ

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值