Decoding of run-length encoding

 

#include<stdio.h> This is a simple C run-length encoding decoding program. After finishing it, I found that there are still many points worth noting,
#include<stdlib.h>                                                                      

int *decode(int *a,int nu) defines a function pointer, and sets two parameters, the first address of an array, and the length of an array. When passing the array, the address and length must be passed.
{         int *p=a,i,sum=0;//The first address of the array is assigned to the pointer p         for(i=0;i<nu/2;i++)//Calculate the length of the entire array         {                 sum+=*(p +1+2*i);         }         printf("%d\n",sum);         int *p2 = (int *)malloc(sum*sizeof(int));//Allocate memory space, space for each unit The length is sizeof(int)         int *b=p2;//The return value of the malloc function, that is, the pointer is assigned to b, and the address is released later.         int *p1=p2;         if(p1 != NULL)//If the memory address space allocated by malloc is not empty, the allocation is successful, and the following operations are performed         {         for(i=0;i<nu;i+=2)         {         int x=*(p+1);//Point to the second number, the number of words         int temp=*p;//Point to the original number, store the original number                 while(x--)//The count value keeps decrementing, Know that all the data is stored
















                {                         *p1++=temp;                 }         p+=2;//This is a transition 2         }         }         else                 return NULL;//If the allocation is not successful, it returns a null pointer         for(i=0;i<sum;i++) //Print the result         {                 printf("%d\n",*p2);                 p2++;         }         free(b);//After using the malloc function, you need to release the function. The parameter must be the original address without change, so use the The way the original address is saved is released         return 0; }














int main()
{         int a[6]={1,3,2,4,3,3}; //There is an encoded ciphertext in the main program, which needs to be parsed into plaintext         decode(a,sizeof(a) /sizeof(a[0]));//Pass in the parameters, the first address of the array and the length of the array.         return 0; }




Guess you like

Origin blog.csdn.net/yyq1w2e3/article/details/6642278