in Programming in C edited by
29,882 views
72 votes
72 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 in C edited by
29.9k views

4 Answers

108 votes
108 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 the array 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
by

4 Comments

A[2][3] may give run time error , as there is a chance it may be out of bound also.
2
2
yeah ,but in question compile time error is asked
0
0
Sir, let C[10] be a 1D array of 10 elements

Then can't we do like

B[1] = C

Will it not copy the elements of array c in 1st row of B.
1
1
11 votes
11 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 

1 vote
1 vote

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

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

Option A is correct✅
Answer:

Related questions