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,1]sinx,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;
}
相关推荐
NAGNIP19 小时前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队19 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja1 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶1 天前
算法 --- 字符串
算法
博笙困了1 天前
AcWing学习——差分
c++·算法
NAGNIP1 天前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP1 天前
大模型微调框架之LLaMA Factory
算法
echoarts1 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Python技术极客1 天前
一款超好用的 Python 交互式可视化工具,强烈推荐~
算法