# GATE2003-2

8.8k 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

edited

$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

edited by
1
can you please explain why I and II wont give compile time error?
52
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 ...
27

(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)
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?
15

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

@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.
1
will A[2] give run time error?
0
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
0

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

Is it correct??

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

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

Option A is correct✅

## Related questions

1
4.6k views
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$
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)]$