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

时间:2023-03-09 14:46:42
C语言--指向多维数组的指针和指针数组
#include <stdio.h>
//void show(char *p[]);
void show(char s[][]);
int main(){
char s[][]={"","abc","xyz"};
char *p[];
//指针数组要循环复制
p[] = s[];
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 = ;
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 ;
} 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[][]='*';
for(i=;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 = ;
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=;k<ct;k++){
puts(ps[k]);
}
return ;
} void str_sort(char *str[], int num){
char *tmp;
int top,seek;
for(top=;top<num-;top++){
for(seek=top+;seek<num;seek++){
if(strcmp(str[top], str[seek]) > ){
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=;i<LMT;i++){
puts(input[i]);
}
} void str_sort(char str[][SIZE], int num){
int i,j;
char tmp[SIZE];
for(i=;i<num-;i++){
for(j=i+;j<num;j++){
if(strcmp(str[i],str[j]) > ){
strcpy(tmp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],tmp);
}
}
}
}