• edited by
43,667 views
117 117 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

7 Answers

Best answer
146 146 votes

$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 an array (second dimension of array $B$) and it cannot be changed as array in $\mathbb{C}$ is a constant pointer.
  • $B[2][3] =$ can take an integer

So, (A) is the answer.

• edited by
16 16 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 

6 6 votes

int *A[10] is an array named A .  A has 10 elements. Every element is pointing to an integer . 

if used as left hand sides of assignment

lets underastand  this line by example;

#include <stdio.h>

int x;       

int main() {
    int *a[10]; 

    a[2] = &x; 

    return 0;
}

 

here, look how we assign the address of x in the pointer which is in the a[2]. now,  a[2] is written left hand side of assignment operator. 

it works. But, B[1] will not work, why?

i.e, we cant assign anything to B[1] .like B[1] = 125 , B[1] = &d. No you cant assign anything. because, B always carry the starting address of 0th index of the 2D array.Its constant.

 

but , B[2][3] = 125; this works perfectly. because, here, you are assigning value to the 3rd index of the 2nd row of 2D array.

 

A[2][3] will also works .

look, an integer pointer can point to 0th index of an array. check the below example;

 

#include <stdio.h>

int main() {
    int *a[10];
    int arr[] = {6, 156, 82, 4, 45};

    a[2] = arr;

    // Modify the value at the fourth position of the array accessed through a[2]
    a[2][3] = 62;

    // Output the modified value
    printf("Modified value: %d\n", a[2][3]);

    return 0;
}

 

So, Correct option is A

2 2 votes
int *A[10] means an array A is a 10 element array with each element being a pointer. Now what does these element points to is not needed.

Option A: A[2] = Yes this can be kept at LHS of an assignment as A[2] means the 3rd element of the array A and that is a address to some element or an entire array (starting address) now can we change the address. Yes! therefore A[2] can be assigned a value.

Option B: A[2][3] = Yes this can be kept at LHS of an assignment as A[2] is the 3rd element and [3] means the third element that this pointer points to but note that this pointer must have 4 block of memory allocated to it.

Option C: B[1] = No this can't be kept at LHS of an assignment as B[1] means out of 2D array B[x][y] we are pointing to 2nd row of this 2D array the entire 2D array, now until you mention which specific element to assign or change in the 2nd row we can't use this.

Option D: B[2][3] = Yes definitely possible, just change the 3rd row 4th element.
0 0 votes
1.A[2]  is an array of pointers

2.A[2][3] we can assign an int value

3.B[1] Address of 2nd row as whole 1D array can't be used to assign int values

4.B[2][3] here also we can assign an int value
Answer:
Position:
Show:

Related questions

65 65 votes
6 answers 6 answers
20.3k
20.3k views
Kathleen asked Sep 17, 2014
20,348 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 = ...
61 61 votes
5 answers 5 answers
14.8k
14.8k views
Kathleen asked Sep 17, 2014
14,757 views
A program consists of two modules executed sequentially. Let $f_1(t)$ and $f_2(t)$ respectively denote the probability density functions of time taken to execute the two ...
2 2 votes
2 2 answers
7.8k
7.8k views
Kathleen asked Sep 17, 2014
7,758 views
Consider the following class definitions in a hypothetical Object Oriented language that supports inheritance and uses dynamic binding. The language should not be assumed...
5 5 votes
0 0 answers
5.8k
5.8k views
Kathleen asked Sep 17, 2014
5,790 views
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...