C Programming - Arrays - Discussion

Discussion Forum : Arrays - Find Output of Program (Q.No. 3)
3.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    void fun(int, int[]);
    int arr[] = {1, 2, 3, 4};
    int i;
    fun(4, arr);
    for(i=0; i<4; i++)
        printf("%d,", arr[i]);
    return 0;
}
void fun(int n, int arr[])
{
    int *p=0;
    int i=0;
    while(i++ < n)
        p = &arr[i];
    *p=0;
}
2, 3, 4, 5
1, 2, 3, 4
0, 1, 2, 3
3, 2, 1 0
Answer: Option
Explanation:

Step 1: void fun(int, int[]); This prototype tells the compiler that the function fun() accepts one integer value and one array as an arguments and does not return anything.

Step 2: int arr[] = {1, 2, 3, 4}; The variable a is declared as an integer array and it is initialized to

a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4

Step 3: int i; The variable i is declared as an integer type.

Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function.

Step 5: for(i=0; i<4; i++) { printf("%d,", arr[i]); } The for loop runs untill the variable i is less than '4' and it prints the each value of array a.

Hence the output of the program is 1,2,3,4

Discussion:
25 comments Page 2 of 3.

Deepak said:   1 decade ago
Can anyone tell why we use fun(4,arr) instead of fun(4,arr[]); when we use fun(4,arr[]) then it will show syntax error.

Tell me how nemory is allocated in this program.

Sanny said:   1 decade ago
Because *p=0 is not in while loop and after finishing the while loop is is giving the value 0 for the *p variable. So there is no effect on the value of array elements.

Gaurav said:   1 decade ago
please someone explain the working of the following statements clearly

while(i++ < n)
p = &arr[i];

first i is incremented or it is compared with n.

Jagpal singh said:   1 decade ago
In function calling base address of array is passing and in function prototype it accept an array as argument.

Why this code won't give any error? please answer.

U.Jhansi sri said:   1 decade ago
Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function. So why this wrote in the program.plz tell me.

PRADEEP said:   1 decade ago
Here i increment to 4 p is assigned as it is post increment. p=&a[4],so it put outside the array limit, .In the main array it is not effected.

Kaustubh said:   1 decade ago
This code causes runtime error. The function func modifies memory which is beyond the scope of the array. This can corrupt the program stack.

Harsha kumar said:   1 decade ago
The code will compile witout any error but it will lead to out of bound condition at run time so the program crashes.

Karthi said:   1 decade ago
Void function doesnt return anything, if the function is int then what will be the output.

Keerthi said:   10 years ago
Thanks @Sanny after reading all this comments you only cleared my doubt thanks a lot.


Post your comments here:

Your comments will be displayed after verification.