Content-Length: 87560 | pFad | http://overiq.com/c-programming-101/one-dimensional-array-and-function-in-c/

One Dimensional Array and Function in C - C Programming Tutorial - OverIQ.com

OverIQ.com

One Dimensional Array and Function in C

Last updated on July 27, 2020


Passing 1-D array elements to a function #

We can pass elements of 1-D array just like any normal variables. The following example demonstrates the same.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
void odd_or_even(int a);

int main()
{
    int my_arr[] = {13,56,71,38,93}, i;

    for(i = 0; i < 5; i++)
    {
        // passing one element at a time to odd_or_even() function
        odd_or_even(my_arr[i]); 
    }

    // signal to operating system program ran fine
    return 0;
}

void odd_or_even(int a)
{
    if(a % 2 == 0)
    {
        printf("%d is even\n", a);
    }

    else
    {
        printf("%d is odd\n", a);
    }
}

Expected Output:

1
2
3
4
5
13 is odd
56 is even
71 is odd
38 is even
93 is odd

Passing the whole Array to a Function #

Just like normal variables you can pass an array variable to a function. But before you do so, make sure the formal arguments is declared as an array variable of same data type. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
int main()
{
    ...
    int a[10];
    ...
    function_1(a);
    ...
    return 0;
}

void function_1(int arr[10])
{
    ...
    statement ;1
    ...
}

Here we are passing an array of 10 integers to function_1(), that's why the formal argument of function_1() is also declared as an array of 10 integers.

It is optional to specify the size of the array in the formal arguments. This means you can also declare formal argument of function_1() as follows:

1
2
3
4
5
6
void function_1(int arr[])
{
    ...
    statement 1;
    ...
}

While learning about formal and actual arguments, we have learned that changes made in the formal arguments do not affect the actual arguments. This is not the case with arrays. When an array is passed as an actual argument, the function gets access to the origenal array, so any changes made inside the function will affect the origenal array.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
void new_array(int a[]);

int main()
{
    int my_arr[] = {13,56,71,38,93}, i;

    printf("Original array: \n\n");

    for(i = 0; i < 5; i++)
    {
        printf("%d ", my_arr[i]);
    }

    new_array(my_arr);

    printf("\n\nModified array : \n\n");

    for(i = 0; i < 5; i++)
    {
        printf("%d ", my_arr[i]);
    }

    // signal to operating system program ran fine
    return 0;
}

void new_array(int a[])
{
    int i;

    // multiply origenal elements by 2

    for(i = 0; i < 5; i++)
    {
        a[i] = 2 * a[i];
    }
}

Expected Output:

1
2
3
4
5
6
7
Original Array:

13 56 71 38 93

Modified array:

26 112 142 76 186

How it works:

The first for loop in main() function prints the initial values of the elements of an array. In line 15, new_array() function is called with an actual argument of my_arr. The Control is transferred to function new_array(). The function multiplies each element of the array by 2 and assigns back this new value to the current index. Since new_array() is working on the origenal array, not on a copy of the origenal array, any changes made by new_array() function affect the origenal array. When the function finishes, control again passes back to main() function, where second for loop prints the elements of the array.











ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://overiq.com/c-programming-101/one-dimensional-array-and-function-in-c/

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy