Computer Science

Array as function argument in c

In C programming language, an array can be passed as a function argument. This allows the function to access and modify the elements of the array. The array is passed by reference, which means that any changes made to the array inside the function will be reflected in the original array outside the function.

Written by Perlego with AI-assistance

3 Key excerpts on "Array as function argument in c"

  • Book cover image for: Learn C Programming from Scratch
    eBook - ePub

    Learn C Programming from Scratch

    A step-by-step methodology with problem solving approach (English Edition)

    int a [] .
  • Array size: The array size is not automatically passed along with it, in C, by default. It is imperative to provide the function with a further parameter specifying the array’s size as a result. The int type argument here denotes the array’s element count and can be of any number of items.
  • Modifying array elements: Any changes made to the elements of an array inside a function will have an impact on the original array when the array is supplied to the function. This is so that the function may work on the memory location of the initial array. The array’s modifications will, therefore, be visible both inside and outside of the method. Example: //Function to input array elements void getarr (int a [], int n) { int i; printf ("Input array elements\n"); for (i = 0; i < n; i++) { scanf ("%d", & a [i]);
    }
    } int main { int main () { int a [10], n; printf ("How many elements to store\n"); scanf ("%d", &n); // when passing arrays to a function we just pass the array name getarr (a, n); return (0); }
  • Similarly, we can use a function to print array elements: void printarr (int a [], int n) { int i; printf ("Here are the array elements\n"); for (i = 0; i < n; i++) { printf ("%d\t", a[i]);
    }
    } Passing arrays to functions enables us to modularize our code and perform operations on arrays without having to rewrite the same code at multiple places. Some array-based programs The following section discusses some array-based programs with the help of examples.
    • Calculate the sum of an array of elements: #include <stdio.h> void sumarr (int a [], int n) { int i, s=0; for (i = 0; i < n; i++) { s = s + a[i];
  • Book cover image for: Scientific Programming: C-language, Algorithms And Models In Science
    eBook - ePub
    • Luciano Maria Barone, Enzo Marinari, Giovanni Organtini, Federico Ricci Tersenghi(Authors)
    • 2013(Publication Date)
    • WSPC
      (Publisher)
    In Section 7.2 we saw that passing an array from one function to another by value is rather problematic. Still, it is fundamental to encode complex problems in a modular way. The solution is again to use pointers as function parameters. Remember that, given for example an array a[10], the name a is a pointer to the array itself and a generic element a[j] has the address (a + j). As was the case for the swap function, we include a pointer, to an array in this case, as a parameter of the function. In this way the entire array becomes accessible to the called function by indirecting the transmitted address and the successive ones. In particular, we can change the values of the array elements. We can also write algorithms on arrays which are not dependent on the length of the array declared in the calling program. Similarly, we can declare a function of the type pointer to an array and return to the calling function an array that has been filled inside the called function. In conclusion, including pointers to arrays as parameters or return arguments of a function extends a lot a program computing capability. Still, this mechanism has a peculiar, rather complicated, syntax in C. This is why we discuss the syntax as well as the mechanism of array passing between functions.
    7.3.1Array in input and output
    Let us consider a function f of the void type receiving in input an integer array int data[10] declared in the calling function. We can declare this array as a formal parameter of f in three different ways all having the same result.
    In either way, the value passed to the function is always the value of the pointer to the array. The first expression, f(int data[10]), clearly shows that the argument of the function is an array, but the nature of the value passed to f is misleading. Moreover, the length of the array data is not passed to the function f, even if it explicitly appears as a parameter. In the second case, the parameter is a pointer to an integer with the name data. There is no explicit indications that it is a pointer to an array. Also in this way the function f does not know the current length of the array data. The third expression clearly indicates that the argument of f is an array because of the square brackets [] . The length of the array is again not indicated though. Thus, an array can be passed to a function as a formal parameter in various syntactical forms. However, in practice the mechanism always consists in passing to the function the current value of the pointer to the array. The information on the current length of the array is never implicitly transmitted. If this information is required (which is often the case) it needs to be communicated explicitly by means of an additional parameter.
    Listing 7.7 The Bubblesort function.
    Let us consider a more realistic case of a function sorting an array, of integers for example, with the Bubblesort algorithm of Section 5.2.1. The function may have two input parameters: the array to be sorted, passed via its pointer; and the number of elements to be sorted, which should be equal to or less than the array length declared in the calling function. The function returns the sorted array without explicitly returning any value. Instead, it directly sorts the array by operating on the pointers to its elements. The function code is shown in Listing 7.7.
  • Book cover image for: ANSI C Programming
    eBook - ePub

    ANSI C Programming

    Learn ANSI C step by step

    10 Arrays
    • What are Arrays A Simple Program Using Array
    • More on Arrays Array Initialisation Bounds Checking Passing Array Elements to a Function
    • Pointers and Arrays Passing an Entire Array to a Function The Real Thing
    • Two Dimensional Arrays Initialising a 2-Dimensional Array Memory Map of a 2-Dimensional Array Pointers and 2-Dimensional Arrays Pointer to an Array Passing 2-D Array to a Function
    • Array of Pointers
    • Three-Dimensional Array
    • Summary
    • Exercise
     
    T he C language provides a capability that enables the user to design a set of similar data types, called array. This chapter describes how arrays can be created and manipulated in C.
    We should note that, in many C books and courses, arrays and pointers are taught separately. I feel it is worthwhile to deal with these topics together. This is because pointers and arrays are so closely related that discussing arrays without discussing pointers would make the discussion incomplete and wanting. In fact, all arrays make use of pointers internally. Hence, it is all too relevant to study them together rather than as isolated topics.

    What are Arrays

    For understanding the arrays properly, let us consider the following program: #include <stdio.h> int main() {   int x;   x = 5;   x = 10;   printf ("x = %d\n", x);   return 0; }
    No doubt, this program will print the value of x as 10. Why so? Because, when a value 10 is assigned to x , the earlier value of x , i.e. 5, is lost. Thus, ordinary variables (the ones which we have used so far) are capable of holding only one value at a time (as in this example). However, there are situations in which we would want to store more than one value at a time in a single variable.
    For example, suppose we wish to arrange the percentage marks obtained by 100 students in ascending order. In such a case, we have two options to store these marks in memory: (a) Construct 100 variables to store percentage marks obtained by 100 different students, i.e. each variable containing one student’s marks.
  • Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.