当前位置:三九宝宝网 → 宝宝教育 → 教学论文 → 正文

C语言函数。利用指针数组实现将n个字符串递增排序后输出

更新:03-18 整理:39baobao.com
字体:

[c语言:数组:不用strcpy函数实现字符串的复制]#include<stdio.h> void Copy_string(char* str1, char* str2); //函数声明 int main() { char str1[20]; char str2[20]; puts("请输入字符串str1:"); gets(str1); //获取从键盘...+阅读

C语言程序如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

#include

#include

#include

#define N 100

#define MAX 100

void sort(char** str,int n)

{

for(int i = 0;i {

for(int j = 0;j {

if(strcmp(str[j], str[j + 1]) >0)

{

char *temp = str[j];

str[j] = str[j + 1];

str[j + 1] = temp;

}

}

}

}

C语言如何用二级指针给N个整数排序

通过二级指针去访问二维数组需要先给二级指针分配等同于二维数组行数的一维数组指针,然后把二维数组的每行首地址赋值给对应位置的一维指针上。之后就可以通过二维指针直接访问了。

参考代码如下,可以看具体注释辅助理解。

12345678910111213141516171819202122232425

#include

用二维指针访问二维数组多用于函数调用。

对于一维数组,如果函数参数为一维指针可以直接用数组名当做函数参数。但是如果函数参数为二维指针,直接用二维数组名做参数会出现访问出错,是因为二维指针和二维数组的访问方式不同造成的,需要如示例代码中做转换。

另外一种常用的方法是利用二维数组的内存连续性将二维数组转为一维数组处理,与本题无关,不做更多描述。

使用函数指针完成数组的排序

#include

typedef int (*FUNC)(int , int);

void sert(int [], const int, FUNC function);

int ascending(int, int);

int descending(int, int);

void swap(int *a, int *b);

int main(void)

{

int n;

int a[10] = {1,9,2,8,3,7,4,6,5,0};

printf("***********************************************\n");

printf("* *\n");

printf("*******************1.升序**********************\n");

printf("* *\n");

printf("*******************2.降序**********************\n");

printf("* *\n");

printf("***********************************************\n");

scanf(" %d", &n);

if (1==n)

{

sert(a, 10, ascending);

}

else if (2==n)

{

sert(a, 10, descending);

}

else

{

printf("输入错误\n");

return 1;

}

for (n = 0; n < 10; ++n)

{

printf("%d ", a[n]);

}

printf ("\n");

return 0;

}

void sert(int a[], const int len, FUNC function)

{

int i, j;

for (i = 0; i < len; ++i)

{

for (j = 0; j < len-1; ++j)

{

if (function(a[j], a[j+1]))

swap(&a[j], &a[j+1]);

}

}

}

int ascending(int a, int b)

{

if (a > b)

return 1;

return 0;

}

int descending(int a, int b)

{

if (a < b)

return 1;

return 0;

}

void swap(int *a, int *b)

{

int tmp;

tmp = *a;

*a = *b;

*b = tmp;

}

本文地址:https://www.39baobao.com/show/29_48870.html

以上内容来自互联网,请自行判断内容的正确性。若本站收录的信息无意侵犯了贵司版权,请联系我们,我们会及时处理和回复,谢谢.

以下为关联文档:

C语言字符串数组字典排序用二维数组记录书名,再声明一个指针数组并使各元素分别指向各书名,然后对指针数组元素按其指向的书名的字典顺序排序。这样比直接拷贝书名的效率会高些。举例如下: //#include...

C语言中的字符指针数组排序完整程序如下: #include<stdio.h> void sort_string(char **p,int n){ int i,j; char s[80]; for (i=0;i<n-1;i++){ for (j=i+1;j<n;j++){ if (strcmp(*(p+i),*(p+j))<0){ st...

c语言字符串数组排序#include <stdio.h> #include<string.h> main() { char*p_str[8]={"Paris","York","London","Shanghai","Edo","Taipei","Beijing","Singapore"},*temp; int i,j; for(i=0;i<7;i++)//冒泡法...

C语言。数组排序函数数组函数排序//#include "stdafx.h"//vc++6.0加上这一行. #include "stdio.h" void Sort(int *p,int n){ int i,j,k; for(i=0;i<10;i++){ for(k=i,j=i+1;j<10;j++) if(p[k]>p[j]) k=j; if(k!...

C语言函数数组指针利用指针求一维数组的数据元素之和#include<stdio.h> int main() { int array[10]; int i, *p; for(p=array, i=0; i<10; i++) scanf("%d", p+i); for(p=array, i=0; i<10; i++) printf("%d ", *(p+i)); return 0...

C语言字符指针数组#include"stdio.h" #include #define A 7 void main() { int i; char **p; p=(char **)malloc(sizeof(char *)); for(i=0;i p[i]=(char*)malloc(sizeof(char)); for(i=0;i { g...

C语言通用函数字符指针数组寻找指定字符串#includenbsp;“stdafx.h“#includenbsp;amp;lt;stdio.hamp;gt;#includenbsp;amp;lt;string.hamp;gt;intnbsp;str2str(constnbsp;charnbsp;*str,nbsp;constnbsp;charnbsp;*s...

c语言字符数组指针#include "stdio.h" #include "string.h" void main() { char a[81]=""; char *p=a; int n,k,pos; puts("input the data"); gets(a); n=strlen(a); puts("the position you want to...

C语言编程题:输入10个字符串将其从大到小排序后输出#include<stdio.h> void swap1(int *p,int n){ int temp; for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++) if(p[i]>p[j]){ temp=p[i]; p[i]=p[j]; p[j]=temp; } } void swa...