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

Assume the following C variable declaration

int *A[10], B[10][10];

Of the following expressions

  1. $A[2]$
  2. $A[2][3]$
  3. $B[1]$
  4. $B[2][3]$

which will not give compile-time errors if used as left hand sides of assignment statements in a C program?

  1. I, II, and IV only
  2. II, III, and IV only
  3. II and IV only
  4. IV only
asked in Programming by Veteran (69k points)
edited by | 2.6k views

1 Answer

+42 votes
Best answer

$A$ is an array of pointers to int, and $B$ is a 2-D array.

$A[2] =$
can take a pointer

$A[2][3] =$
can take an int

$B[1] =$
$B[1]$ is the base address of array and it cannot be changed as array in C is a constant pointer.

$B[2][3] =$
can take an integer

So, (A) is the answer.

answered by Veteran (346k points)
edited by
can you please explain why I and II wont give compile time error?
int *A[10];

Here A is an array of pointers.

So, A[2] =
Here we are assigning to the third element of the array, so the RHS must be an address as we are having a pointer array.

A[2][3] =
This is equivalent to *(A[2] + 3). This is again valid and is going to the third integer address from A[2] (A[2] + 3 * sizeof(int)) and the RHS is copied to that address. For this to work properly. A[2] must have an array of memory (of size at least 3) for int allocated. Even otherwise no compilation error- only runtime error.
A[2] is a second element of array A or third element of A in C ...

(I) :-  A[10] is an array of pointer of integers value (every element of array A[10] is a pointer which points to integer elements ) ,
A[2] is a third element (which is a pointer itself of an integer) of array A[10] , 
if we write as ..

A[2] = y ; we can change the element of an array , so there is no problem .

(II) :- A[2] is a third element of array A[10] , which is a pointer to an integer element ;

A[2][3] will be fourth integer element pointed by third element(which is an address itself..i.e.... A[2] ) ,
if we write as ...

A[2][3] = y ;  we can change an integer element , so there is no problem .

(III) :- B[10][10] is an 2-D array , where each element is integer type , B[10][10] have total 10 row , 10 column , and total 10*10= 100 integer type elements .

B[1] is selecting address of second row of array B[10][10] ,and if we write this as ..

B[1] = y ;

address of array = y ; fails because of we can not change address(offset address) of an array . so there is problem .

(IV) :- B[2][3] is selecting particular element of 2-D array B[10][10]  , and if we write this as..
B[2][3] = y ;

x = y ;   {let B[2][3] = x (an element)}

so we can change the element x as y {y is an integer type element} of 2-D array B[10][10] ,  there is no problem .



Note :- example for array A , take 10 char type strings  (I am taking char instead of int, we can take int also) 
string1= newdelhi (starting address 1000)
string2 =hyderabad (starting address 2000)
string3= bangalore (starting address 3000) 
string4 =Rennes (starting address 4000)
string5 = France (starting address 5000)
string10 = iisc (starting address 10000)
now array A[10] became ,
char * A[10] , let starting address of array A is 11000


array element  1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
array index number 0 1 2 3 4 5 6 7 8 9

A[2] = 3000  , we can change as it an array element ,we can change (by another string address) ,
A[2][3] = 'g' is an element of string3 ('bangalore') , we can change by another char element .

correct me.............???

yeah exactly, you can.
@Arjun sir, for A[2][3], is it not necessary, that A[2] must be a pointer to an integer array of size atleast 4, but as per the question int*A[10],A[2] is simply an integer pointer, would it create any sort of problem?

A[2][3] = *(*(A+2) + 3)

Now, A is declared as int *A[10]- Array of pointers to int which is const pointer to pointer to int. So, *A is pointer to int.

A+2 = A + size of(*A) = A + sizeof(int *)

So, *(A+2) will give the element (which is also a pointer) in the 3rd address from beginning of A. 

Now *(A+2) + 3, here the type of left side is pointer to int, and its pointed to type is int. So, we add 3* sizeof(int) to the beginning of *(A+2) and get to the 4th int element.

Yes, you are right A[2] must be allocated space for at least 4 int elements. 

I have a doubt that if we try to assign a floating number to any of the LHS or assign a pointer type variable to LHS. Won't it give an error in such cases as RHS is nothing specific in the question?
@arjun u should take an example.That will be helpful to understand
@Arjun Sir, so for A[2][3] =integer ,A[2] must contain a pointer to an array of atleast 4 intergers ,Am i correct?
sir but A[2][3] will not always work and may result in segmentation fault (may) because we r point to just an integer we r not pointing to an integer array where we can gaurentee that consecutive memory locations are available
what is meant by just an integer ?

Can any one clarify it ?

Related questions

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

33,704 questions
40,252 answers
38,860 users