retagged by
353 views
1 votes
1 votes
int fun(int arr[]); int fun(int arr[2]); Both the statements are same??? HELP
retagged by

1 Answer

1 votes
1 votes

Declaration is all about just knowing the data type.
It is enough to know that, it is an array of int and it will return int .

So both statements will work same.

Run the below code:




#include <stdio.h>

// Function declaration: int fun(int arr[])
int fun(int arr[])
{
    int sum = 0;
    int i;

    // Calculate the sum of array elements
    for (i = 0; arr[i] != '\0'; i++)
    {
        sum += arr[i];
    }

    return sum;
}

// Function declaration: int fun(int arr[2])
int fun2(int arr[2])
{
    int sum = 0;
    int i;

    // Calculate the sum of array elements
    for (i = 0; arr[i] != '\0'; i++)
    {
        sum += arr[i];
    }

    return sum;
}

int main()
{
    int arr1[] = {1, 2, 3, 4, 5};  // Array of any size
    
    // Call the first function
    int result1 = fun(arr1);
    printf("Result 1: %d\n", result1);

    // Call the second function
    int result2 = fun2(arr1);
    printf("Result 2: %d\n", result2);

    return 0;
}

OUTPUT:

Result1: 15 

Result2: 15

Related questions

5 votes
5 votes
3 answers
1
0 votes
0 votes
0 answers
2
Pranavpurkar asked Sep 27, 2022
599 views
Consider a string is stored in a word addressable memory where each word is of 4 Bytes. If the string starts at 100 and ends at 104 then which of the following can be the...
0 votes
0 votes
0 answers
3