How to Pass a 1D Array as a Function Arguments in C?

This C Tutorial Explains One-Dimensional Arrays as Function Arguments in C Language with Example(s).

C is a modular programming language meaning that it allows us to organise large programs into self-contained modules called functions. Each function performs some dedicated task. For example,

void copy_str(char *dstr, char *sstr)
{
    for (*dstr++ = *sstr++ != '\0')
		;
}

Above function copies source string to destination string. We are familiar with using functions which we had passed scalar variables. For example,

int main(void)
{
    int num1 = 10, num2 = 20;
 
    swap(num1, num2);    /* 'num1' and 'num2' are actual parameters */
    printf("After calling \'swap()\', \'num1\' and \'num2\' are %d and "
           "%d\n", num1, num2);
    return 0;
}
 
void swap(int n, int m)    /* 'n' and 'm' are formal parameters */
{	
    int temp;
 
    /* logic for swapping two nos. */
    temp = n;
    n = m;
    m = temp;
 
    printf("In \'swap()\', \'n\' and \'m\' are %d and %d\n", n, m);
}

When function ‘swap()’ was called, actual values of ‘num1’ and ‘num2’ were passed to the function ‘swap()’ and initialized to the formal variables ‘n’ and ‘m’ in the function header. Function ‘swap()’, swapped them up and displayed the result. However, they aren’t swapped in ‘main()’ function. Because, ‘swap()’ performed on the copies of actual arguments.

advertisement
advertisement

In order values be swapped in ‘main()’ as well, should we call ‘swap()’ and pass the memory addresses of two variables so function ‘swap()’ performs on actual values instead of copies of them. Accordingly, formal arguments in function header must be declared of type pointer. So, function ‘swap()’ takes shape as:

void swap(int *pi1, int *pi2) /* 'pi1' and 'pi2' are both pointer-to-int */
{
    int temp;
 
    temp = *pi1;
    *pi1 = pi2;
    *pi2 = temp;
}

Let’s now take an example of one-dimensional array,

    float avg_life[10];

As array is a vector it can contain specified number of elements. In array ‘avg_life[10]’, there are 10 floats which are garbage values by default. Like scalars, if we call some function to pass arrays as values, entire array is to be copied to function parameter, wasting lots of time and memory, the critical resources of the system. Further this will slow down execution. In order to prevent wastage of critical resources, to speed up execution of the program, an array irrespective of its dimension is passed by address than by value. Called function receives the address of the array and accesses the array. Let’s see how address of an array is passed to the called function.

In order to receive the address of an array in called function, type of array must be known in advance and accordingly pointer of same type must be declared as formal parameter. For example, type of array ‘avg_life[10]’ is a pointer-to-float. Therefore, in the called function,

void clear(float *fp, int arr_size) /* 'fp' declared as pointer-to-float */
{
    int i;
 
    for (i = 0; i < arr_size; i++)
        *fp++ = 0;                     /* clears the array */
}

What did you observe in above function? Only address of start of the array is passed to the function into ‘*fp’ rather than entire array is copied. Function ‘clear()’ using pointer-to-float ‘*fp’, no. of elements in the array and using pointer arithmetic accessed each array element and set it to 0. Thus, ‘clear()’ performed on the actual values of the array avg_life[]’.

advertisement

There are situations where we need to prevent original data from accidental alterations by the called function, for example, copying a source string to destination string, we can define function prototype and function header as:

void copy_str(char *, const char *);    /* prototype */
 
/* formal argument 'sstr' is ptr-to-const-char */
void copy_str(char *dstr, const char *sstr)
{
    for (*dstr++ = *sstr++ != '\0')
        ;
}

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.