Page 176 - C-Language
P. 176
Chapter 24: Function Parameters
Remarks
In C, it is common to use return values to denote errors that occur; and to return data through the
use of passed in pointers. This can be done for multiple reasons; including not having to allocate
memory on the heap or using static allocation at the point where the function is called.
Examples
Using pointer parameters to return multiple values
A common pattern in C, to easily imitate returning multiple values from a function, is to use
pointers.
#include <stdio.h>
void Get( int* c , double* d )
{
*c = 72;
*d = 175.0;
}
int main(void)
{
int a = 0;
double b = 0.0;
Get( &a , &b );
printf("a: %d, b: %f\n", a , b );
return 0;
}
Passing in Arrays to Functions
int getListOfFriends(size_t size, int friend_indexes[]) {
size_t i = 0;
for (; i < size; i++) {
friend_indexes[i] = i;
}
}
C99C11
/* Type "void" and VLAs ("int friend_indexes[static size]") require C99 at least.
In C11 VLAs are optional. */
void getListOfFriends(size_t size, int friend_indexes[static size]) {
size_t i = 0;
for (; i < size; i++) {
https://riptutorial.com/ 152

