C语言程序设计基础实验报告8

时间:2025-05-11

实验8 指针 一、实验目的

1、通过实验进一步掌握指针的概念,会定义和使用指针变量; 2、能正确使用数组的指针和指向数组的指针变量; 3、能正确使用字符串的指针和指向字符串的指针变量;

二、实验内容和步骤

1、改错

(1)指出下面程序错误的原因.

main( )

{ int x=10,y=5,*px,*py;

px=py; px=&x; py=&y;

printf("*px=%d,*py=%d,*px,*py); }

(2)下面的程序试图有如下运行结果:

which style you want to :

Capital ( c ) or uncapital ( a ):c ↙ COMPUTER

Which sytle you want to :

Capital ( c ) or uncapital ( a ) : u↙ computer

请调试下面的程序,使其有如上执行结果.

#include main ()

{ char s*;char c;

printf("which style you want to :ln"); printf("capital ( c ) or uncapital(a):"); c=getchar();

if(c='c')strcpy(s,"COMPUTER"); else strcpy(s,"computer"); put(s); }

#include<stdio.h> void main()

{ int x=10,y=5,*px,*py; px=&x; py=&y;

printf("px=%d,py=%d",*px,*py); }

#include<stdio.h> #include<string.h>

#include<stdlib.h> void main()

{ char *s;char c; printf("which style you want to :\n"); printf("capital(c) or uncapital(a):"); c=getchar();

s=calloc(20, sizeof(char)); //分配存储空间

if(c=='c') }

strcpy(s,"computer");

strcpy(s,"COMPUTER"); else

puts(s);

(3)下面的程序能获得上述运行结果吗

main()

{char *S="COMPUTER"; char c;

printf("which style you want to \n"); printf("capital (c) or uncapital(u);"); c=getchar(); if(c='c') put(s); else {

s="computer"; puts(s); } }

(4)设计一个C程序,用以测试下列各表达式的运算.

#include<stdio.h> void main() { }

char *s="COMPUTER"; char c;

printf("which style you want to:\n"); printf("capital(c) or uncapital(u);"); c=getchar(); if(c=='c') { }

puts(s); s="computer"; puts(s); else

(1) y=*px++ (2) y=*++py (3) y=(*py)++ (4) y=--*ppx++

(5)想使指针变量pt1指向a 和b 中的大者,pt2指向小者,以下程序能否实现此目的

swap(int *p1,int *p2) {int *p;

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

main() {int a,b;

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

pt1=&a;pt2=&b; if(a<b)swap(pt1.pt2);

printf("%d,%d\n",*pt1,*pt2);

}

#include<stdio.h>

void swap(int *p1,int *p2) { int p; p=*p1;*p1=*p2;*p2=p; }

void main()

{ int a,b,*pt1,*pt2;

2、程序填空:

}

scanf("%d,%d",&a,&b); pt1=&a; pt2=&b; if(a<b) swap(&pt1,&pt2);

printf("%d,%d\n",*pt1,*pt2);

(1)一维数组和指针。分别采用下标法、数组名法和指针法访问数组元素,求出10 个数中的最大值。 下标法: #include<stdio.h> void main() {

int a[10],i,max; for(i=0;i<=9;i++) }

scanf ("%d",&a[i]); if(max<a[i]) max=a[i]; for(i=0;i<=9;i++)

printf ("MAX=%d\n",max);

数组名法: #include<stdio.h> void main() { }

字符数组实现: #include<stdio.h> void main() {

}

char s[20];int i; gets(s);

for(i=0;i<=19;i++) i=0;

while(s[i]!='\0') {

printf("%c",s[i]); i++;} if(s[i]>='A'&&s[i]<='Z')

s[i]=s[i]+'a'-'A';

int a[10],i,max; for(i=0;i<=9;i++)

scanf("%d",a+i); if(max<*(a+i)) for(i=0;i<=9;i++) max=*(a+i);

printf ("MAX=%d\n",max);

指针法: #include<stdio.h> void main() { }

字符指针变量实现: #include<stdio.h> void main() { }

char s[20];char *p; scanf("%s",s); p=s;

while(*p!='\0') {

if(*p>='A'&&*p<='Z')

*p=*p+('a'-'A'); p++;}

int a[10]; int *p,max;

for(p=a;p<(a+10);p++)

scanf("%d",p); if(max<*p) max=*p; for(p=a;p<(a+10);p++) printf ("MAX=%d\n",max);

(2)字符串和字符指针。输入一个字符串,将其中的大写字母转换成小写字母,然后输出。

puts(s);

提示:scanf ()输入时遇到空格认为字符串结束,用gets()输入时只有遇到回车才认为字符串结束。如键入any boy并回车,则

3、编程序并上机调试运行程序(都要求用指针处理)。

(1) 输入三个整数,按由小到大的顺序输出,然后将程序改为:输入三个字符串,按由小到大顺序输出。 #include <stdio.h> void sort(int *a,int *b,int *c) { int t=0; if (*a>*b)

{ t=*a;*a=*b;*b=t;} if (*a>*c)

{ t=*a;*a=*c;*c=t;} if (*b>*c)

{ t=*b;*b=*c;*c=t;} }

void main() { int a=0,b=0,c=0;

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

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

#include<stdio.h> #include<string.h>

void swap(char *s1[20],char *s2[20]) { char *t;

t=*s1;*s1=*s2;*s2=t; }

void main()

{ char str1[20],str2[20],str3[20]; char *p1[20],*p2[20],*p3[20]; gets(str1);gets(str2);gets(str3); strcpy(p1,str1); strcpy(p2,str2); strcpy(p3,str3); if(strcmp(p1,p2)>0) swap(p1,p2); if(strcmp(p1,p3)>0) swap(p1,p3); if(strcmp(p2,p3)>0) swap(p2,p3);

puts(p1);puts(p2);puts(p3); }

(2)将一个3×3的矩阵转置,用一函数实现之。

在主函数中用scanf函数输入以下矩阵元素: l 3 5 7 9 11 13 15 19

将数组名作为函数实参,在执行函数的过程中实现矩阵转置,函数 …… 此处隐藏:2082字,全部文档内容请下载后查看。喜欢就下载吧 ……

C语言程序设计基础实验报告8.doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:7 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:29元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219