码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-04-14 14:24:36      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

 

 

#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] != \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\n",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] != \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;
                        }
                }
        }
}

 

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

标签:

原文地址:http://www.cnblogs.com/bai-jimmy/p/4424651.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!