成信大2021自动化专业-平时自主学习-C语言改错题解题参考-整理第06页

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

第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;
			}
		}
	}
}

考查要点:

  1. 二维数组,里面的元素是一个一维数组
  2. 一维指针数组,里面的元素是一个个指针,而一维指针数组的数组名,也是一个地址,对它取解引用,取出来还是一个指针【一级指针】,对它进行偏移,仍然是一个指针【二级指针】
  3. 这里的对串的排序,本质上,是用指针的指向进行的排序,交换的是指向,不是指向的内容,但比较判断时,一定是拿内容来进行判断
  4. 排序算法本身,这里采用的是选择排序法

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;
}

考查要点:

  1. 指针和对应的地址要类型一致,整型指针,指向整型变量的地址
  2. 有了指向的指针,就是地址

D1035.c

原文件

#include<stdio.h>

int main(void)
{
   
   
	/*********Fou
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值