dome1:
#include
#include
//输出数组元素
void printStrArr(char **arr,int len)
{
if (arr==NULL||len==0)
{
return;
}
for (int i = 0; i < len;i++)
{
printf("%s\n", *(arr + i));
//printf("%s\n", arr[i]);
}
return;
}
//排序
void sortStrArr(char **arr, int len)
{
int i = 0, j = 0;
char *tmp = NULL;
if (arr == NULL || len == 0)
{
return;
}
for (i = 0; i < len; i++)
{
for (j = i; j < len;j++)
{
if (strcmp (arr[i],arr[j])>0)
{
//这里交换的式指针的值,不是指针指向的数据。只是改变了数组元素指针的指向
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
int main(void)
{
char *strArr[] = {
"asdfr", "cccccc", "bnnnn", "322222" };
int num = 0;
num = sizeof(strArr) / sizeof(strArr[0]);//求数组中元素个数
printStrArr(strArr, num);
sortStrArr(strArr, num);
printf("\n\n排序之后\n\n");
printStrArr(strArr, num);
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
//输出数组元素
void printStrArr(char arr[10][30], int len)
{
if (arr == NULL || len == 0)
{
return;
}
for (int i = 0; i < len; i++)
{
printf("%s\n", arr[i]);
//printf("%s\n", arr[i]);
}
return;
}
//排序
void sortStrArr(char arr[10][30], int len)
{
int i = 0, j = 0;
char tmp[30] = {
0 };
if (arr == NULL || len == 0)
{
return;
}
for (i = 0; i < len; i++)
{
for (j = i; j < len; j++)
{
if (strcmp(arr[i], arr[j])>0)
{
memset(tmp, 0x00, sizeof(tmp));
strcpy(tmp, arr[i]); //这里是对数组元素的值进行交换
strcpy(arr[i], arr[j]);
strcpy(arr[j], tmp);
}
}
}
}
int main(void)
{
char strArr[10][30] = {
"hasdfr", "ccccccv", "bnnnn", "122222" };
int num = 4;
printStrArr(strArr, num);
sortStrArr(strArr, num);
printf("\n\n排序之后\n\n");
printStrArr(strArr, num);
system("pause");
return 0;
}
从上面两个例子可以看出 char *strArr[] 与 char strArr[10][30] 在作为函数参数时的不同。
关于求数组元素个数 char *strArr[] 类型可以通用 下面
num = sizeof(strArr) / sizeof(strArr[0]);//求数组中元素个数
但是 char strArr[10][30] 类型不能用这种方法:
int main(void)
{
char strArr[10][30] = {
"hasdfr", "ccccccv", "bnnnn", "122222" };
int len1 = sizeof(strArr);
int len2 = sizeof(strArr[0]);
int num = len1 / len2;
printf("len1=%d ; len2=%d ; num=%d \n", len1, len2, num);
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
//输出数组元素
void printStrArr1(char **arr, int len)
{
if (arr==NULL||len==0)
{
return;
}
for (int i = 0; i < len;i++)
{
printf("%s\n", *(arr + i));
//printf("%s\n", arr[i]);
}
return;
}
//排序
void sortStrArr1(char **arr, int len)
{
int i = 0, j = 0;
char *tmp = NULL;
if (arr == NULL || len == 0)
{
return;
}
for (i = 0; i < len; i++)
{
for (j = i; j < len; j++)
{
if (strcmp(arr[i], arr[j])<0)
{
tmp = arr[i]; //这里交换的式指针的值,不是指针指向的数据。
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
char ** get_mem(int elements,int eleLength)
{
char **tmp = NULL;
if (elements<=0||eleLength<=0)
{
return NULL;
}
tmp = (char **)malloc(sizeof(char*)*elements);
if (tmp==NULL)
{
return NULL;
}
for (int i = 0; i < elements;i++)
{
tmp[i] = (char *)malloc(sizeof(char)*eleLength);
if (tmp[i]!=NULL)
{
memset(tmp[i], 0x00, eleLength);
sprintf(tmp[i], "%C%C%C%d%d%d", 65 + i, 65 + i, 65 + i, 65 + i, 65 + i, 65 + i);
}
}
return tmp;
}
int main()
{
char** arr = NULL;
int ele_count = 10;//元素个数
int ele_length = 30;//单个元素大小
arr = get_mem(ele_count, ele_length);
if (arr==NULL)
{
printf("存储空间开辟失败!\n");
}
printStrArr1(arr, ele_count);
printf("\n\n排序之后\n\n");
sortStrArr1(arr, ele_count);
printStrArr1(arr, ele_count);
//释放
if (arr!=NULL)
{
for (int i = 0; i < ele_count;i++)
{
if (arr[i]!=NULL)
{
free(arr[i]);
arr[i] = NULL;
}
}
free(arr);
}
system("pause");
return 0;
}
1:char *p1[] = { "asd", "xcv", "123" };
2:char p2[3][5] = { "123", "456", "789" };
3:手动开辟空间
char ** buf = NULL;
buf = (char **)malloc(sizeof(char*) * 3);
for (int i = 0; i < 3;i++)
{
buf[i] = (char *)malloc(sizeof(char) * 5);
sprintf(buf[i],"%d%d%d", i + 1, i + 2, i + 3);
}