The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+14 votes

Consider the following declaration of a two-dimensional array in C:

char $a[100][100]$;

Assuming that the main memory is byte-addressable and that the array is stored starting from memory address $0$, the address of $a [40][50]$ is

  1. $4040$
  2. $4050$
  3. $5040$
  4. $5050$
asked in Programming by Veteran (68.9k points)
edited ago by | 1.2k views

1 Answer

+23 votes
Best answer

answer is (B)

In C, arrays are always stored in row-major form.

 Formula to evaluate 2-D array's location is:----

                                        $loc(a[i][j]) = BA + [(i-lb_1)\times NC+(j-lb_2)]\times c$


$BA$ - Base Address
$NC$ - no. of columns
$c$ - memory size allocated to data type of array

          $a[lb_1 \cdots ub_1] [lb_2\cdots ub_2]$

here $BA=0,  NC  =100,    c=1,    a[0.....99][0......99]$ so $lb_1=0 , lb_2=0$

$loc(a[40][50])= 0+ [ (40-0)\times 100 + (50-0)]\times 1$
                           $= 0+[4000+50]\times 1 = 4050$

answered by Loyal (3.3k points)
edited ago by
Why column major used here,and not row major?
very well explained
i think by these ans may be contradict but by default we use  column majaor

.its a naive approach :)
why coloum major order plese explain because if we solve this from row major order then anwer will come out to be 5040 (Option C) which is also given in the option ?
Why element size is 1?
char --> 1
Who is using column major? It is row-major which is mandated in C.

@Arjun sir....any source plss

B is the answer, and RMO is used here not CMO.

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

32,693 questions
39,293 answers
36,701 users