文章目录
第06页
题面如下:

题解如下:
D1033.c
原文件
/* 排序字符串,若输入:this test terminal,以下程序的输出结果为:
terminal
test
this
*/
#include<stdio.h>
#include<string.h>
#define MAXLINE 20
void sort(char *pstr[]);
int main(void)
{
int i;
char *pstr[3], str[3][MAXLINE];
for (i=0; i<3; i++)
{
pstr[i] = str[i];
}
printf("Please input:");
for (i=0; i<3; i++)
{
/*********Found************/
scanf("%s", pstr+i);
}
sort(pstr);
printf("output:");
for (i=0; i<3; i++)
{
/*********Found************/
printf("%s\n", pstr);
}
return 0;
}
void sort(char *pstr[])
{
int i, j;
char *p;
for (i=0; i<3; i++)
{
for (j=i+1; j<3; j++)
{
/*********Found************/
if (strcmp(pstr+i, pstr+j) > 0)
{
p = *(pstr+i);
*(pstr+i) = *(pstr+j);
*(pstr+j) = p;
}
}
}
}
改后文件
/* 排序字符串,若输入:this test terminal,以下程序的输出结果为:
terminal
test
this
*/
#include<stdio.h>
#include<string.h>
#define MAXLINE 20
void sort(char *pstr[]);
int main(void)
{
int i;
char *pstr[3], str[3][MAXLINE];
for (i=0; i<3; i++)
{
pstr[i] = str[i];
}
printf("Please input:");
for (i=0; i<3; i++)
{
/*********Found************/
scanf("%s", pstr[i]);
}
sort(pstr);
printf("output:");
for (i=0; i<3; i++)
{
/*********Found************/
printf("%s\n", pstr[i]);
}
return 0;
}
void sort(char *pstr[])
{
int i, j;
char *p;
for (i=0; i<3; i++)
{
for (j=i+1; j<3; j++)
{
/*********Found************/
if (strcmp(pstr[i], pstr[j]) > 0)
{
p = *(pstr+i);
*(pstr+i) = *(pstr+j);
*(pstr+j) = p;
}
}
}
}
考查要点:
- 二维数组,里面的元素是一个一维数组
- 一维指针数组,里面的元素是一个个指针,而一维指针数组的数组名,也是一个地址,对它取解引用,取出来还是一个指针【一级指针】,对它进行偏移,仍然是一个指针【二级指针】
- 这里的对串的排序,本质上,是用指针的指向进行的排序,交换的是指向,不是指向的内容,但比较判断时,一定是拿内容来进行判断
- 排序算法本身,这里采用的是选择排序法
D1034.c
原文件
#include<stdio.h>
int main(void)
{
int a;
/*********Found************/
float *p;
p = &a;
/*********Found************/
scanf("%d", &p);
printf("a=%d\n", *p);
return 0;
}
改后文件
#include<stdio.h>
int main(void)
{
int a;
/*********Found************/
int *p;
p = &a;
/*********Found************/
scanf("%d", p);
printf("a=%d\n", *p);
return 0;
}
考查要点:
- 指针和对应的地址要类型一致,整型指针,指向整型变量的地址
- 有了指向的指针,就是地址
D1035.c
原文件
#include<stdio.h>
int main(void)
{
/*********Fou

本文提供了一系列C语言编程练习题及其解答,涵盖指针、数据结构及文件操作等内容,通过实例帮助读者深入理解并掌握相关概念和技术。
最低0.47元/天 解锁文章
3816

被折叠的 条评论
为什么被折叠?



