edited by
10,495 views
42 votes
42 votes

C program is given below:

# include <stdio.h>
int main ()
{
        int i, j;
        char a [2] [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}};
        char b [3] [2];
        char *p = *b;
        for (i = 0; i < 2; i++) {
              for (j = 0; j < 3; j++) {
              *(p + 2*j + i) = a [i] [j];
              }
        }
}

What should be the contents of the array b at the end of the program?

  1. $\text{a    b}$
    $\text{c    d}$
    $\text{e    f}$
     
  2. $\text{a    d}$
    $\text{b    e}$
    $\text{c    f}$
     
  3. $\text{a    c}$
    $\text{e    b}$
    $\text{d    f}$
     
  4. $\text{a    e}$
    $\text{d    c}$
    $\text{b    f}$
edited by

2 Answers

Best answer
58 votes
58 votes

The correct answer is option (B).

first integer type two variables declared $i$ and $j$

then an integer type $2-d$ array $a[2][3]$ is declared and initialized and $2-d$ array $b[3][2]$ is created but not initialized. i.e 

            address     value                       address         value

$a[0]0[]$   $2000$         $a$               $b[0][0]$   $3000$          garbage value

$a[0][1]$   $2001$         $b$               $b[0][1]$   $3001$          garbage value

$a[0][2]$   $2002$         $c$               $b[1][0]$   $3002$          garbage value

$a[1][0]$   $2003$         $d$               $b[1][1]$   $3003$          garbage value

$a[1][1]$   $2004$         $e$               $b[2][0]$   $3004$          garbage value

$a[1][2]$   $2005$         $f$                $b[2][1]$   $3005$          garbage value

now the char type pointer is declared and the base address of  array  $b$ is put in it. so $p=3000$

now the for loop is started where $i$ is initialized to $0$ ,so 

$i=0 : i<2$ (true)

      $j=0; j<3$ (true)

          $*(3000+2*0+0) =a [0][0]  \Rightarrow *(3000) = a$

            $j++$ 

         $j=1; j<3$ (true)

         $ *(3000+2*1+0) =a [0][1]  \Rightarrow *(3002) = b$

           $ j++$

         $j=2; j<3$ (true)

          $*(3000+2*2+0) =a [0][2]  \Rightarrow  *(3004) = c$

           $ j++$

         $j=3; j<3$ (false)

    $i++$

$i=1 : i<2$ (true)

       $j=0; j<3$ (true)

          $*(3000+2*0+1) =a [1][0]  \Rightarrow *(3001) = d$

            $j++$ 

         $j=1; j<3$ (true)

          $*(3000+2*1+1) =a [1][1]  \Rightarrow *(3003) = e$

            $j++$

         $j=2; j<3$ (true)

         $ *(3000+2*2+1) =a [1][2]  \Rightarrow  *(3005) = f$

            $j++$

         $j=3; j<3$ (false)

    $i++$

now the values in array $b$ is 

 $b[0][0]$   $3000$           $a$

 $b[0][1]$   $3001$           $d$

 $b[1][0]$   $3002$           $b$

 $b[1][1]$   $3003$           $e$

 $b[2][0]$   $3004$           $c$

 $b[2][1]$   $3005$           $f$

Hence, the output will be (B) choice.

Note:

*(p + 2*j + i)

$p +$ size of inner dimension $* j + i$, hence is same as $p[j][i]$. Hence with this statement we can identify that the code is transposing the matrix $a$ and storing in $b$ using pointer $p$. 

edited by
4 votes
4 votes

Both will behave similarly.

$char *p=b$

$char *p=*b$


But $char *p=b+1\ \&\ char*p=*b+1$ will behave differently.

Answer:

Related questions

30 votes
30 votes
4 answers
2
Ishrat Jahan asked Oct 28, 2014
9,600 views
What is the output printed by the following C code?# include <stdio.h int main () { char a [6] = "world"; int i, j; for (i = 0, j = 5; i < j; a [i++] = a [j ]); printf ("...