C语言--指向多维数组的指针和指针数组

#include <stdio.h>
//void show(char *p[]);
void show(char s[][10]);
int main(){
        char s[3][10]={"123","abc","xyz"};
        char *p[10];
        //指针数组要循环复制
        p[1] = s[1];
        char (*ps)[];
        ps = s;
}
#include <stdio.h>
#include <string.h>
#define SIZE 80
#define LMT 3
#define HALT " "

/**
 * 字符串排序函数函数原型声明
 */
//如果直接传递二维数组,函数里边只能显示数组元素,不能更改元素位置
void str_sort(char str[][SIZE],int num);
//void str_sort(char *str,int num);

int main(){
        char input[LMT][SIZE];
        //指针数组,要分别为每个元素赋值,指向多维数组的指针只需要为指针变量赋值即可
        //指针数组初始化的时候要指定数组的大小,指向多维数组的指针只分配指针变量的内存空间即可
        char *ps[LMT];
        int ct = 0;
        int k;

        while(ct<LMT && gets(input[ct]) != NULL && input[ct][0] != ''){
                ps[ct] = input[ct];
                ct++;
        }
        //str_sort(ps,ct);
        str_sort(input,ct);
        //puts("Here is the res list:");
        //for(k=0;k<ct;k++){
        //      puts(ps[k]);
        //}
        return 0;
}

void str_sort(char str[][SIZE], int num){
        //char *tmp;
        //int top,seek;
        //for(top=0;top<num-1;top++){
        //      for(seek=top+1;seek<num;seek++){
        //              if(strcmp(str[top], str[seek]) > 0){
        //                      tmp = str[top];
        //                      str[top] = str[seek];
        //                      str[seek] = tmp;
        //              }
        //      }
        //}
        int i;
        str[1][1]='*';
        for(i=0;i<num;i++){
                printf("%s
",str[i]);
        }
}
#include <stdio.h>
#include <string.h>
#define SIZE 80
#define LMT 3
#define HALT " "

/**
 * 字符串排序函数函数原型声明
 */
void str_sort(char *str[],int num);

int main(){
        char input[LMT][SIZE];
        char *ps[LMT];
        int ct = 0;
        int k;

        while(ct<LMT && gets(input[ct]) != NULL && input[ct][0] != ''){
                ps[ct] = input[ct];
                ct++;
        }
        str_sort(ps,ct);
        puts("Here is the res list:");
        for(k=0;k<ct;k++){
                puts(ps[k]);
        }
        return 0;
}

void str_sort(char *str[], int num){
        char *tmp;
        int top,seek;
        for(top=0;top<num-1;top++){
                for(seek=top+1;seek<num;seek++){
                        if(strcmp(str[top], str[seek]) > 0){
                                tmp = str[top];
                                str[top] = str[seek];
                                str[seek] = tmp;
                        }
                }
        }
}

二维数组作为函数参数

#include <stdio.h>
#include <string.h>
#define LMT 3
#define SIZE 80
void str_sort(char str[][SIZE], int num);
int main(){
        char input[LMT][SIZE];
        int ct;
        while(ct < LMT && gets(input[ct]) != NULL && input[ct][0] != ''){
                ct++;
        }
        str_sort(input,LMT);
        int i;
        for(i=0;i<LMT;i++){
                puts(input[i]);
        }
}

void str_sort(char str[][SIZE], int num){
        int i,j;
        char tmp[SIZE];
        for(i=0;i<num-1;i++){
                for(j=i+1;j<num;j++){
                        if(strcmp(str[i],str[j]) > 0){
                                strcpy(tmp,str[i]);
                                strcpy(str[i],str[j]);
                                strcpy(str[j],tmp);
                        }
                }
        }
}
原文地址:https://www.cnblogs.com/bai-jimmy/p/4424651.html