C language two level pointer to three memory models

  • 2020-09-16 07:43:33
  • OfStack

Level 2 Pointers are more difficult to use than level 1 Pointers, but the difficulty lies in the mixing of Pointers and arrays. The different types of level 2 Pointers are defined quite differently when used

First memory model char *arr[]

If defined as follows


char *arr[] = {"abc", "def", "ghi"};

This model is the first memory model of 2-level Pointers and should be understood in this way: an array of Pointers (char * []) is defined, and each element of the array is an address.
When using an intermediate quantity to manipulate an element, the intermediate quantity should be defined as


char *tmp = NULL;

If you want to print this array, you can use the following function


int printAarray(char **pArray, int num)
{
	int i = 0;
	if (pArray == NULL)
	{
		return -1;
	}
	for (i = 0; i < num; i++)
	{
		printf("%s \n", pArray[i]);
	}
	return 0;
}

Second memory model char arr[][]

If defined as follows


char arr[3][5] = {"abc", "def", "ghi"};

This model is the second memory model of 2-level Pointers. It defines a 2-dimensional array with 3 (5 char) space storage variables.

When using an intermediate quantity to manipulate an element, the intermediate quantity should be defined as


char tmp[5] = { 0 };

If you want to print this array, you can use the following function


nt printAarray(char pArray[][5], int num)
{
	int i = 0;
	if (pArray == NULL)
	{
		return -1;
	}
	for (i = 0; i < num; i++)
	{
		printf("%s \n", pArray[i]);
	}
	return 0;
}

The third memory model, char **arr

If defined as follows


char **arr = (char *)malloc(100 * sizeof(char *));//char arr[400]
arr[0] = (char *)malloc(100 * sizeof(char));//char buf[100]
arr[1] = (char *)malloc(100 * sizeof(char));
arr[2] = (char *)malloc(100 * sizeof(char));
strcpy(arr[0], "abc");
strcpy(arr[1], "def");
strcpy(arr[2], "ghi");
 ... 
for(int i = 0; i < 3; i++)
 if(arr[i] != NULL)
  free(arr[i]);
free(arr);

This model is the second memory model of 2-level pointer, which should be understood in this way: a 2-level pointer is defined, and the 2-level pointer is the pointer pointing to the pointer. In fact, it opens up 100 pointer Spaces and stores 100 addresses. This is a simplified version of the first

When using an intermediate quantity to manipulate an element, the intermediate quantity should be defined as


char *tmp = NULL

If you want to print this array, you can use the following function


{
	int i = 0;
	if (pArray == NULL)
	{
		return -1;
	}
	for (i = 0; i < num; i++)
	{
		printf("%s \n", pArray[i]);
	}
	return 0;
}

example

The data of the first memory model is sorted, and the results are put into the third memory model


#include "stdio.h"
#include "string.h"
#include "stdlib.h"

char **SortArrayAndGen3Mem(const char ** const myArray1, int num, char *str, int *myNum)
{
	char **p = NULL;
		p= (char **)malloc(num*sizeof(char *));
	if (myArray1==NULL || str==NULL|| myNum==NULL)
	{
		printf(" Pass in parameter error \n");
		p = NULL;
		goto END;
	}
	*myNum = num;
	for (int i = 0; i < num;i++)
	{
		p[i] = NULL;
		p[i] = (char)malloc(50 * sizeof(char));
		memset(p[i], 0, sizeof(p[i]));
		if (p[i]==NULL)
		{
			printf(" Memory allocation error! \n");
			goto END;
		}
		strcpy(p[i], myArray1[i]);
	}
	char *tmp;
	for (int i = 0; i < num; i++)
	{
		for (int j = i + 1; j < num; j++)
		{
			if (strcmp(p[i],p[j])>0)
			{
				char *tmp = p[i];
				p[i] = p[j];
				p[j] = tmp;
			}
		}
	}
	for (int i = 0; i < num; i++)
	{
		printf("%s \n", myArray1[i]);
	}

END:
	return p;
}

// Memory free function 

void main()
{
	int i = 0;
	char **myArray3 = NULL;
	int num3 = 0;
	// The first 1 Seed memory model 
	char *myArray[] = {"bbbbb", "aaaaa", "cccccc"};
	char *myp = "111111111111";

	myArray3 = SortArrayAndGen3Mem(myArray, 3, myp, &num3);

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

	system("pause");
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

char **SortArrayAndGet3Mem(const char* const myArray1,int num,char *str,int *myNum);
int getArray(char ***newp,int num) ;
int freeArray(char ***newpfree,int num);
int sortTArray(char *p, int num);

void main()
{
	char **myArray3=NULL;
	int num3=0;
	char *myArray[]={"bbbb","aaa","cccc"};
	char *myp="111111111";
	myArray3=SortArrayAndGet3Mem(myArray,3,myp,&num3);
	system("pause");
}


char **SortArrayAndGet3Mem(const char** const myArray1,int num,char *str,int *myNum)
{
	int ret=0;
	char **p=NULL;
	int i=0;
	char **p1=NULL;
	p1=(char **)myArray1;
	ret=getArray(&p,num +1);
	for (i=0;i<num;i++)
	{
		strcpy(p[i],p1[i]);
	}
	strcpy(p[i], str);
	ret=sortTArray(p,num +1);
	for (i=0;i<num +1;i++)
	{
		printf("%s\n",p[i]);
	}
	ret=freeArray(&p,num +1);
	*myNum = num +1;
	return p;
}

int getArray(char ***newp,int num) 
{
	int i=0;
	int ret=0;
	char **tmp = NULL;
	tmp = (char **)malloc(num*sizeof(char *));
	for (i=0;i<num;i++)
	{
		tmp[i]=(char*)malloc(sizeof(char)*100);
	}
	*newp = tmp; //
	return 0;
}

//
int freeArray(char ***newpfree,int num)
{
	char **p=NULL;
	int i=0;
	int ret=0;
	p=*newpfree;
	for (i=0;i<num;i++)
	{
		free(p[i]);
	}
	free(p);
	*newpfree = NULL; //
	return ret;
}

//int sortTArray(char ***Arraystr, int num)
int sortTArray(char **Arraystr, int num)
{
	int i , j = 0; 
	for (i=0; i<num; i++)
	{
		for (j=i+1; j<num; j++)
		{
			if (strcmp((Arraystr)[i],(Arraystr)[j])>0)
			{
				char tmp[100];
				strcpy(tmp,(Arraystr)[i]);
				strcpy((Arraystr)[i],(Arraystr)[j]);
				strcpy((Arraystr)[j],tmp);
			}
		}
	}
	for (i=0;i<num;i++)
	{
		printf("%s\n",(Arraystr)[i]);
	}
	return 0;
}

The above is the detailed content of C language -2 level pointer 3 memory models, more about C language -2 level pointer 3 memory models, please pay attention to other related articles on this site!


Related articles: