C语言-指针数组,指向数组的指针,指向函数的指针,指向函数的指针数组的定义与应用

指针数组,指向数组的指针,指向函数的指针,指向函数的指针数组的定义与应用

c 复制代码
	//指针数组
	int* p[32];
	//指向数组的指针
	int(*p)[32];
	//指向函数的指针
	int (*p)(int);
	//指向函数的指针数组
	int (*p[10])(int);

一.指针数组的应用:

题目要求:使用指针数组作为函数参数,实现对10个字符串进行排序

1.输入:

c 复制代码
lisi
hahaha
hehehe
helloa
leihoua
lisi
nihaoa
wangwu
ajax
bureau

2.程序:

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
#include<math.h>
int main()
{
	int i;
	char str[10][32];
	char *pstr[10];
	for (i = 0; i < 10; i++)
	{
		//pstr[i] = str[i];
		*(pstr + i) = *(str + i);
		//scanf("%s", pstr[i]);
		scanf("%s", *(pstr+i));//捕捉字符串到第i个子数组中
	}
	void str_sort(char **str,int n);
	str_sort(pstr,10);
	printf("----------------\n");
	for (i = 0; i < 10; i++)
	{
		printf("%s\n", *(str + i));
	}
	printf("----------------\n");
	for (i = 0; i < 10; i++)
	{
		//printf("%s\n", pstr[i]);
		printf("%s\n", *(pstr + i));
	}
}
void str_sort(char **str,int n)
{
	int i, j;
	for (i = 0; i < n - 1; i++)
	{
		for (j = 0; j < n -1 - i; j++)
		{
			//if (strcmp(str[j], str[j+1]) > 0)
			if(strcmp(*(str+j),*(str+j+1))>0)
			{
				//char temp[32];
				/*strcpy(temp, str[j]);
				strcpy(str[j], str[j + 1]);
				strcpy(str[j + 1], temp);*/
				char* temp = *(str + j);
				*(str + j) = *(str + j + 1);
				*(str + j + 1) = temp;
			}
		}
	}
}

二、指向数组的指针的应用

题目要求:使用指向数组的指针作为函数参数,实现对10个字符串进行排序

1.输入:

c 复制代码
lisi
hahaha
hehehe
helloa
leihoua
lisi
nihaoa
wangwu
ajax
bureau

2.程序:

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
void str_sort(char(*p)[32], int n)
{
	int i, j;
	for (i = 0; i < n - 1; i++)
	{
		for (j = 0; j < n - 1 - i; j++)
		{
			if (strcmp(*(p + j), *(p + j + 1)) > 0)
			{
				char temp[20];
				strcpy(temp, *(p + j));
				strcpy(*(p + j), *(p + j + 1));
				strcpy(*(p + j + 1), temp);
			}
		}
	}
}
int main()
{
	int i;
	char str[10][32];
	for (i = 0; i < 10; i++)
	{
		scanf("%s", *(str + i));
	}
	str_sort(str,10);
	for (i = 0; i < 10; i++)
	{
		printf("%s\n", *(str + i));
	}
}

三、指向函数的指针的应用

题目要求:使用指向函数的指针写一个能够实现加减乘除的计算器

程序:

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<math.h>
double Add(double x, double y)
{
	return(x + y);
}
double Del(double x, double y)
{
	return(x - y);
}
double Mul(double x, double y)
{
	return(x * y);
}
double Chu(double x, double y)
{
	return(x / y);
}
void cal(double (*p)(double, double))
{
	double x1, x2;
	printf("请输入两个数\n");
	scanf("%lf %lf", &x1, &x2);
	double res = p(x1, x2);
	printf("结果为%.2lf", res);
	
}


int main() 
{
	printf("请输入要选择的计算方式1.加法,2.减法,3.乘法,4.除法:");
	int choice;
	scanf("%d", &choice);
	switch (choice)
	{
	case 1:
		cal(Add);
		break;
	case 2:
		cal(Del);
		break;
	case 3:
		cal(Mul);
		break;
	case 4:
		cal(Chu);
		break;
	default:
		break;
	}
}

四、指向函数的指针数组的应用

题目要求:写一个用矩形法求定积分的通用函数,分别求积分区间为0,1sinx,cosx,e的x方的定积分

程序:

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<math.h>

double integral(double num1, double num2, int count, double (*handler)(double))
{
	double dx = (num2 - num1) / count;
	double res = 0;
	for (int i = 0; i < count; i++)
	{
		double dh = handler(num1 + dx * i);
		res += dx * dh;
	}
	return res;
}
int main()
{
	printf("请输入积分区间[num1,num2]:");
	double num1, num2;
	scanf("%lf%lf", &num1, &num2);
	printf("请输入分割数量:");
	int count;
	scanf("%d", &count);
	printf("请输入被积分函数<1-sin,2-cos,3-exp>:");
	int choice;
	scanf("%d", &choice);

	double(*var[4])(double) = { NULL,sin,cos,exp };
	double res = integral(num1, num2, count, var[choice]);
	printf("定积分结果:%lf\n", res);
	return 0;
}
相关推荐
SoftLipaRZC7 分钟前
顺序表的应用:通讯录项目与经典算法实战
算法
8Qi87 分钟前
LeetCode 583. 两个字符串的删除操作
算法·leetcode·职场和发展·动态规划
tigershang11 分钟前
卡尔曼滤波:不确定世界中的最优估计
人工智能·算法·机器学习
凡人叶枫12 分钟前
Effective C++ 条款02:宁可以编译器替换预处理器
java·linux·c语言·开发语言·c++
m0_7381207223 分钟前
渗透测试基础——PHP 序列化数据结构与反序列化机制详解
android·服务器·网络·数据结构·安全·php
一个儒雅随和的男子25 分钟前
限流算法详细剖析
java·服务器·算法
周杰伦fans35 分钟前
AutoCAD2016经典模式不见了-设置回14版本前的经典工作空间
服务器·c语言·前端
少司府1 小时前
C++进阶:红黑树
开发语言·数据结构·c++·b树·二叉树·红黑树
caimouse1 小时前
Reactos 第 5 章 进程与线程 — 5.1 概述
c语言·windows·架构
工业胶粘剂技术2 小时前
单组分高温环氧结构胶 K-EP280 完整技术参数与工程选型分析
算法·制造