retagged by
30,127 views
40 votes
40 votes

Consider the following $\text{ANSI C}$ program.

#include <stdio.h>
int main() 
{
    int arr[4][5];
    int  i, j;
    for (i=0; i<4; i++) 
    ​​​​​​{
        for (j=0; j<5; j++) 
        {
            arr[i][j] = 10 * i + j;
        }
    }
    printf(“%d”, *(arr[1]+9));
    return 0;
}

What is the output of the above program?

  1. $14$
  2. $20$
  3. $24$
  4. $30$
retagged by

11 Answers

0 votes
0 votes

Answer – Option C

After executing of for loop, Array will be – 

0 1 2 3 4
10 11 12 13 14
20 21 22 23 24
30 31 32 33 34

Now ,

printf(“%d”, *(arr[1]+9));

arr[1] means address of first integer of second row ( as Index starting from 0)  . arr[1]+9 = arr[1]+9*sizeof(int) 

 

i.e arr[2][4] element which is 24

0 votes
0 votes
  1. for (i=0; i<4; i++)
  2. ​​​​{
  3.     for (j=0; j<5; j++)
  4.      {
  5.        arr[i][j] = 10 * i + j;
  6.       }
  7. }
  8. After execution of the code array is stored as :
  9. 0 1 2 3 4
    10 11 12 13 14
    20 21 22 23 24
    30 31 32 33 34

    coming to this line :printf(“%d”, *(arr[1]+9));

  10. In one dimensional array arr[1] means the second element in the array .Because array index index is start from 0.

  11. But in two arrays arr[1] means it is pointing to 2nd row of containing 5 integer elements

  12. → in 2D array ,rows addresses are represented as :

  13. 1st row address means *(arr+0)

  14. 2nd row address means *(arr+1)

  15. 3rd  row address means *(arr+2)   and so on…

  16. arr[1]+9 means *(arr+1)+9*(sizeof integer),here it is pointing to the arr[2][4] ,

  17. because after 9 elements of the first row is arr[2][4] and count start from 0th element of the first row.

  18. *(arr[1]+9)=*(*(arr+2)+4) it means the element which contains in that address i.e arr[2][4] =24

0 votes
0 votes
The question asked us to find *(a[1] + 9 ) .

a[1] refers to the 2nd row of the matrix and *(a[1]) refers to the first element of the 2nd row of the matrix , so we have to find the element after 9 positions after the first position of the 2nd row of the matrix .

Hence the answer comes to be 24
Answer:

Related questions

11 votes
11 votes
4 answers
1
6 votes
6 votes
1 answer
3