The use of array as a function parameter and return value in C language simple introduction _c language

Source: Internet
Author: User
Tags int size

function is used as an argument by an array

If you want to use a one-dimensional array as the parameter of a function, you must declare a function-form argument, which produces a similar result with one of all three declarative methods in the following three ways, because each way tells the compiler that an integer pointer will be received. In a similar way, you can use multidimensional array form parameters.

Way-1
The form parameter is the pointer below. The next chapter will learn what pointers are.

void MyFunction (int *param)
{
.
.
.
}
Way-2
The size of the array parameter as follows:

void MyFunction (int param[10])
{
.
.
.
}
Way-3
Array parameters as variable sizes as follows:

void MyFunction (int param[])
{
.
.
.
}
Example
Now, consider the following function, which will require an array as another parameter, and, depending on the parameters passed, it returns the average of the number of values in the group, as follows:

Double getaverage (int arr[], int size)
{
 int  i;
 Double avg;
 Double sum;

 for (i = 0; i < size; ++i)
 {
  sum + = Arr[i]
 ;

 } avg = sum/size;

 return avg.
}

Now, let's call the above function as follows:

 #include <stdio.h>
 
/* Function declaration *
/double getaverage (int arr[], int size);

int main ()
{/
  * is int array with 5 elements */
  int balance[5] = {1000, 2, 3, m};
  Double avg;

  /* Pass Yiibaier
  to the array as a argument * * avg = getaverage (balance, 5);
 
  /* Output The returned value *
  /printf ("Average value is:%f", avg);
  
  return 0;
}

When the above code is compiled and executed, it produces the following results:

Average value is:214.400000

As you can see, the length of the array is not important, as long as the C language function does not check the bounds of formal arguments.

function returns an array
C language Programming does not allow the return of the entire array as a parameter to the function. However, you can return an array of pointers by specifying an array name without an index. If you want to return a one-dimensional array from a function, you must declare that you return a pointer, as in the following example:

int * MyFunction ()
{
.
.
.
}
The 2nd to remember is that the C language does not advocate that local variables are returned outside the function, so the local variable must be defined as a static variable.

Now, consider the following function, which produces 10 random digits and returns them using an array, and calls this function as follows:

#include <stdio.h>

/* function to generate and return random numbers
*/int * Getrandom ()
{
 static int r[10];
 int i;

 /* Set the seed */
 Srand ((unsigned) time (NULL));
 for (i = 0; i < ++i)
 {
   R[i] = rand ();
   printf ("r[%d] =%d
", I, R[i]);

 return r;
}

/* Main function to call above defined function
/int main ()
{/
  * a yiibaier to a int */
  int *p;
   int i;

  p = getrandom ();
  for (i = 0; i < i++)
  {
    printf ("* (P +%d):%d
", I, * (P + i));
  }

  return 0;
}

When the above code is compiled and executed, it produces some of the following:

R[0] = 313959809
r[1] = 1759055877
r[2] = 1113101911
r[3] = 2133832223 r[4
] = 2073354073 r[5
] = 167288 147
R[6] = 1827471542
r[7] = 834791014 r[8
] = 1901409888 r[9
] = 1990469526
* (p + 0): 313959809
* (P + 1): 1759055877 * (P +
2): 1113101911
* (P + 3): 2133832223
* (P + 4): 2073354073
* (P + 5): 167 288147
* (P + 6): 1827471542 * (P +
7): 834791014
* (P + 8): 1901409888
* (P + 9): 1990469526

Related Article

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.