C语言二级指针做函数输入的三种内存模型总结

C语言二级指针做函数输入的三种内存模型

1. 第一种内存模型

char** p <==> char* p[]
在栈区分配内存

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

//void printArray(char* p[], int len) ==
void printArray(char** p, int len)
{
	for (int i = 0; i < len; i++)
	{
		printf("%s ", p[i]);
	}
	printf("\n");
}

void sortArray(char** p, int len)
{
	char* tmp;
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = i + 1; j < len; j++)
		{
			if (strcmp(p[i], p[j]) > 0)
			{
				tmp = p[i];
				p[i] = p[j];
				p[j] = tmp;
			}
		}
	}
}

int main()
{
	char *p[] = { "asdhaj", "11111", "22222", "33333" };
	int len = sizeof(p) / sizeof(p[0]);

	sortArray(&p, len);
	printArray(&p, len);

	printf("\n");
	system("pause");
	return 0;
}

2.第二种内存模型

char a[][30]
在栈区分配内存

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

void sortArr(char a[][30], int len)
{
	char tmp[30];
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = i + 1; j < len; j++)
		{
			if (strcmp(a[i], a[j]) > 0)
			{
				strcpy(tmp, a[i]);
				strcpy(a[i], a[j]);
				strcpy(a[j], tmp);
			}
		}
	}
}

void printArr(char a[][30], int len)
{
	for (int i = 0; i < len; i++)
	{
		printf("%s ", a[i]);
	}
	printf("\n");
}

int main()
{
	char a[][30] = { "111111", "222", "34345", "4265367" };
	int len = sizeof(a) / sizeof(a[0]);

	sortArr(a, len);
	printArr(a, len);

	printf("\n");
	system("pause");
	return 0;
}

3.第三种内存模型

char** p
在堆区分配内存。

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

char** getMemory(int n)
{
	char** buf = (char**)malloc(n * sizeof(char*));
	if (buf == NULL)
	{
		return NULL;
	}

	for (int i = 0; i < n; i++)
	{
		buf[i] = (char*)malloc(30 * sizeof(char));
		char str[30];
		sprintf(str, "mem%d%d", i, i);
		strcpy(buf[i], str);
	}

	return buf;
}

void printArr(char** p, int len)
{
	if (p == NULL)
		return -1;

	for (int i = 0; i < len; i++)
	{
		printf("%s ", p[i]);
	}
	printf("\n");
}

void freeBuf(char** p, int len)
{
	for (int i = 0; i < len; i++)
	{
		free(p[i]);
		p[i] = NULL;
	}

	if (p != NULL)
	{
		free(p);
		p = NULL;
	}
}

int main()
{
	char** buf = NULL;
	buf = getMemory(10);

	printArr(buf, 10);

	freeBuf(buf, 10);

	printf("\n");
	system("pause");
	return 0;
}

4.指针的综合强化训练

free()部分未写出,请自行加上

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

char** sortArr(char** p1/*in*/, int num1, char(*p2)[30]/*in*/, int num2, /* char*** myp3,(out) */int* num3/*in&out*/)
{
	int i, j, tmpNum = num1 + num2;
	char* tmp = NULL;
	char** buf = (char**)malloc(tmpNum * sizeof(char*));
	for (i = 0; i < num1; i++)
	{
		buf[i] = (char*)malloc((strlen(p1[i])+1));
		if (buf[i] == NULL)
			return NULL;
		strcpy(buf[i], p1[i]);
	}

	for (j = 0; j < num2; i++, j++)
	{
		buf[i] = (char*)malloc((strlen(p2[j]) + 1));
		if (buf[i] == NULL)
			return NULL;
		strcpy(buf[i], p2[j]);
	}

	for (i = 0; i < tmpNum - 1; i++)
	{
		for (j = i + 1; j < tmpNum; j++)
		{
			if (strcmp(buf[i], buf[j]) > 0)
			{
				tmp = buf[i];
				buf[i] = buf[j];
				buf[j] = tmp;
			}
		}
	}
	//*myp3 = buf;
	*num3 = tmpNum;

	return buf;
}

int main()
{
	int ret = 0;
	char** p3 = NULL;
	int size3 = 0;
	char* p1[] = { "aaaa", "bbbbb", "ccccc" };
	char p2[][30] = { "11111", "2222", "333333" };
	int size1 = sizeof(p1) / sizeof(p1[0]);
	int size2 = 3;

	p3 = sortArr(p1, size1, p2, size2, &size3);

	for (int i = 0; i < size1 + size2; i++)
	{
		printf("%s ", p3[i]);
	}
	printf("\n");

	printf("\n");
	system("pause");
	return 0;
}

你可能感兴趣的:(C)