C语言-使用指向指针的指针的方法对5个字符串排序并输出

使用指向指针的指针的方法对5个字符串排序并输出

注意:

在C语言中,二维字符数组的数组名实际上不是指向二级指针的,而是隐含地形成了一种特殊的"间接引用"或者说"偏移地址"。当你声明一个二维字符数组如char arr[3][4]时,虽然它的名字arr本身并不是直接指向另一个指针,但实际上它代表了数组首元素在内存中的起始地址。

具体程序:

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
	char str[5][30];
	char* pstr[5];
	int i;
	for (i = 0; i < 5; i++)
	{
		pstr[i] = str[i];
		scanf("%s", *(pstr + i));
	}
	void sort(char** pstr);
	printf("------------\n");
	sort(pstr);
	for (i = 0; i < 5; i++)
	{
		printf("%s\n", *(pstr + i));
	}
	return 0;
}
void sort(char** pstr)
{
	int i, j;
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 4 - i; j++)
		{
			if (strcmp(*(pstr + j), *(pstr + j + 1)) > 0)
			{
				char* temp = *(pstr + j);
				*(pstr + j) = *(pstr + j + 1);
				*(pstr + j + 1) = temp;
				/*char temp[30];
				strcpy(temp, *(pstr + j));
				strcpy(*(pstr + j), *(pstr + j + 1));
				strcpy(*(pstr + j + 1), temp);*/
			}
		}
	}
	
}

运行结果:

相关推荐
dingdingfish7 小时前
Bash学习 - 第3章:Basic Shell Features,第5节:Shell Expansions
开发语言·学习·bash
rainbow68897 小时前
C++开源库dxflib解析DXF文件实战
开发语言·c++·开源
deepxuan7 小时前
Day7--python
开发语言·python
禹凕7 小时前
Python编程——进阶知识(多线程)
开发语言·爬虫·python
铉铉这波能秀7 小时前
LeetCode Hot100数据结构背景知识之字典(Dictionary)Python2026新版
数据结构·python·算法·leetcode·字典·dictionary
蜡笔小马8 小时前
10.Boost.Geometry R-tree 空间索引详解
开发语言·c++·算法·r-tree
IOsetting8 小时前
金山云主机添加开机路由
运维·服务器·开发语言·网络·php
唐梓航-求职中8 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#
仟濹8 小时前
【算法打卡day3 | 2026-02-08 周日 | 算法: BFS】3_卡码网99_计数孤岛_BFS | 4_卡码网100_最大岛屿的面积DFS
算法·深度优先·宽度优先
Ll13045252988 小时前
Leetcode二叉树part4
算法·leetcode·职场和发展