edited by
19,031 views
64 64 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}$

5 Answers

Best answer
82 82 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
6 6 votes

Both will behave similarly.

$char *p=b$

$char *p=*b$


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

0 0 votes

For people getting option D

The most important point to remember for all future aspirants - 

char *p = *b

MISTAKE - 

b points to the base address (and hence the whole first row) of array b. So doing p+2 Means you would point p to the LAST row in the result array, which would now contain the element "b".

This Means you would get the answer as Option D.

But *b points to the first element - NOT the entire first row.

So doing *(p+2) would give the element "c" in the second iteration.

0 0 votes
char b[3][2]. The second number (2) is the width (columns).

char *p = *b $\Rightarrow$ Pointer *p starts at the first seat of matrix b.

\[
*(p + 2j + i) \quad \text{means } 2 \text{ is the width of } b
\]

In C, the variable multiplied by the width is the Row

Remember:
\[
(\text{Width} \times \text{Index}_1 + \text{Index}_2) \;\Rightarrow\; \text{Index}_1 \text{ is the Row.}
\]

So:
\[
*(p + 2j + i) = a[i][j]
\]
and since the coefficient of $j$ is the width (2),  
\[
2j + i \;\Rightarrow\; j \text{ is the Row of } b.
\]

Therefore:
\[
a[i][j] \text{ moves to } b[j][i].
\]

Basically: $i,j \to j,i$ $\Rightarrow$ Transpose

 

Hence:

     Row 1 of $a$ $(a, b, c)$ $\Rightarrow$ Column 1 of $b$
     Row 2 of $a$ $(d, e, f)$ $\Rightarrow$ Column 2 of $b$

Option B = Correct

 

 
Answer:
Position:
Show:

Related questions

103 103 votes
4 answers 4 answers
24.9k
24.9k views
Ishrat Jahan asked Oct 28, 2014
24,935 views
Consider the C program given below. What does it print?#include <stdio.h int main () { int i, j; int a [8] = {1, 2, 3, 4, 5, 6, 7, 8}; for(i = 0; i < 3; i++) { a[i] = a[i...
58 58 votes
4 answers 4 answers
18.4k
18.4k views
Ishrat Jahan asked Oct 28, 2014
18,379 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 ("...
32 32 votes
7 answers 7 answers
17.8k
17.8k views
Ishrat Jahan asked Oct 28, 2014
17,807 views
Consider the C program below. What does it print?# include <stdio.h # define swap1 (a, b) tmp = a; a = b; b = tmp void swap2 ( int a, int b) { int tmp; tmp = a; a = b; b ...
12 12 votes
2 answers 2 answers
7.3k
7.3k views
Ishrat Jahan asked Oct 27, 2014
7,347 views
Match the programming paradigms and languages given in the following table. Paradigms Languages(I)Imperative(a)Prolog(II)Object Oriented(b)Lisp(III)Functional(c)C, Fortra...