The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+30 votes
4.2k views

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 (59.7k points)
edited by | 4.2k views

3 Answers

+48 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 (367k points)
edited by
+1
can you please explain why I and II wont give compile time error?
+33
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.
+2
A[2] is a second element of array A or third element of A in C ...
+18

(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.............???

0
yeah exactly, you can.
+1
@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?
+11

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. 

0
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?
0
@arjun u should take an example.That will be helpful to understand
0
@Arjun Sir, so for A[2][3] =integer ,A[2] must contain a pointer to an array of atleast 4 intergers ,Am i correct?
0
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
0
what is meant by just an integer ?

Can any one clarify it ?
+1

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

0
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.
0
will A[2] give run time error?
+2 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]);
getchar();

Output: 12 15

So Correct Option : A 

answered by Junior (599 points)
+2 votes
answered by Active (3.5k points)
Answer:

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

44,196 questions
49,668 answers
163,545 comments
65,817 users