function returns more than one value (c + +)

Source: Internet
Author: User

When we are dealing with an array, we often encounter problems such as entering an array, and an element of a group, and returning the number of rows and columns of the Element. This requires returning more than two sets of values and varying the number of groups. These types of functions have two problems in the C language Program. first, the function can only return one value (pointer); second, if the value is returned as a pointer, the memory size referred to by the pointer is not Determined. The following addresses these two issues separately and then takes a comprehensive look at Them.

1 functions that return multiple values

1.1 Using pointer parameters to record values

Do not use the return value of the function directly with the Pointer. A pointer is added to the input entry of the function to hold the record multiple Values. As shown below, a[n][m] is the array to be processed, s is the number to be searched, and pointer C records the value and returns the original Function.

1 intFind_num1_1 (inta[n][m],intSint*c)2{3     inti,j;4      for(i=0;i<n;i++)5          for(j=0;j<m;j++)6             if(a[i][j]==s)7{8c[0]=i;c[1]=j;9                 return1;Ten} one     return0; a}

1.2 Returning pointers with functions

You can directly define a function that returns a pointer, return the pointer, and then take the pointer to the desired VALUE. As follows, the function returns a pointer that records the result that needs to be Output.

1 int*find_num1_2 (inta[n][m],intS2{3     int*co;4     inti,j;5 6Co= (int*)malloc(thesizeof(int));7      for(i=0;i<n;i++)8          for(j=0;j<m;j++)9             if(a[i][j]==s)Ten{ oneco[0]=i;co[1]=j; a                 returnCo -} -      free(co); theco=null; -     returnCo -}
But here is a problem, if found in the corresponding s in a row and column, the front with Mallloc allocated memory area can not be released, thereby wasting memory.

2 Returning the dynamic pointer

Because the values in the array are the same number of uncertainties, using large arrays is too wasteful of memory, so use the dynamic memory Approach. There are two ways to return the same correspondence, but here I only refer to the method of using the pointer parameter to record the value for returning the dynamic Pointer. What we need to note here is that when allocating memory for a pointer, only one allocation can be made (i'm not sure, I'd like to know that the spectators can provide some guidance). In this way, we must first define a function to return the number of identical items before allocating memory to it. The functions are as follows:

1 intFIND_NUM2 (inta[n][m],intSint(*c) [2])2{3     inti,j,n=0;4      for(i=0;i<n;i++)5          for(j=0;j<m;j++)6             if(a[i][j]==s)7{8c[n][0]=i,c[n][1]=j;9n++;Ten} one     returnN a} - intSame_num (inta[n][n],intS -{ the     inti,j,n=0; -      for(i=0;i<n;i++) -          for(j=0;j<m;j++) -             if(a[i][j]==s) +++n; -     returnN +}

Memory is allocated in the parent function, which makes it easier to free up memory.

  1  n=same_num (a,s);  2  cc= (int  (*) [2]) malloc  (n*2*sizeof  (int )) ;  3  if  (find_num2 (a,s,cc)!=0)  4  while  (n--)  5  printf  ("number is in the (%d,%d) \ n ", cc[n][0],cc[n][1]);  6  else   7
     printf  ("can ' t find it\n ");  8  free  (cc); 

This makes the requirements perfectly achievable, and the output data can be used for other function processing.

Complete code see: HTTPS://GITHUB.COM/ELIKE-YPQ/C_AND_CPLUSPLUS_STUDY/BLOB/MASTER/LABORATORY/MULTIRETURN.C

function returns more than one value (c + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.