Log In
44 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
in Programming
edited by

3 Answers

67 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.

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 ?

@Venkat Sai.. Yes, A[2][3] can cause a segfault if A[2] points just to an int or an array of int of size less than 4.
But segfault is run time error. In the question, compile-time error is asked. It won't create any problem at compile time. 

array A contains an array of pointer. So, 'A[2]=' means we can change its content....but here content of A[2] is an address , so can we change it??? Explain.
will A[2] give run time error?
Had the question been "no compile time and runtime error" the answer would have been I and IV only . II might give segmentation error if the address (* (A + 2) + 3) is outside our access rights. Imagine something like this

int * A[5] ;

A[2] = (int*) malloc (sizeof(int)) ; (choice 1)

A[2] = (int*) malloc (n * sizeof(int)) ; (choice 2 , where n > 3)

Choice 1 might give segmentation fault but not so for choice 2


A is array of ten pointers which points to an integer but in your example it points to string.

Is it correct??

4 votes

int main()

int *A[10], B[10][10]; 
int C[] = {12, 11, 13, 14};

/* No problem with below statement as A[2] is a pointer 
    and we are assigning a value to pointer */
A[2] = C; 

/* No problem with below statement also as array style indexing 
    can be done with pointers*/
A[2][3] = 15;

/* Simple assignment to an element of a 2D array*/
B[2][3] = 15;

printf("%d %d", A[2][0], A[2][3]);

Output: 12 15

So Correct Option : A 

0 votes
B[1] in this case we can not change the address, so it will give compile time error.,,

Option A is correct✅

Related questions

25 votes
6 answers
Consider the C program shown below: #include<stdio.h> #define print(x) printf("%d", x) int x; void Q(int z) { z+=x; print(z); } void P(int *y) { int x = *y + 2; Q(x); *y = x - 1; print(x); } main(void) { x = 5; P(&x); print(x); } The output of this program is: $12 \ 7 \ 6$ $22 \ 12 \ 11$ $14 \ 6 \ 6$ $7 \ 6 \ 6$
asked Sep 17, 2014 in Programming Kathleen 4.6k views
2 votes
3 answers
Consider the following class definitions in a hypothetical Object Oriented language that supports inheritance and uses dynamic binding. The language should not be assumed to be either Java or C++, though the syntax is similar. Class P { Class Q subclass of P { void f(int i) { void f(int i ... of y to P. The output produced by executing the above program fragment will be 1 2 1 2 1 1 2 1 2 2 2 2
asked Sep 17, 2014 in Programming Kathleen 2k views
4 votes
0 answers
Consider the following logic program P $\begin{align*} A(x) &\gets B(x,y), C(y) \\ &\gets B(x,x) \end{align*}$ Which of the following first order sentences is equivalent to P? $(\forall x) [(\exists y) [B(x,y) \land C(y)] \Rightarrow A(x)] \land \neg (\exists x)[B(x,x)]$ ... $(\forall x) [(\forall y) [B(x,y) \land C(y)] \Rightarrow A(x)] \land (\exists x)[B(x,x)]$
asked Sep 17, 2014 in Programming Kathleen 1.8k views
20 votes
3 answers
Which of the following statements is FALSE? In statically typed languages, each variable in a program has a fixed type In un-typed languages, values do not have any types In dynamically typed languages, variables have no types In all statically typed languages, each variable in a program is associated with values of only a single type during the execution of the program
asked Sep 16, 2014 in Programming Kathleen 2.9k views