edited by
29,831 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
edited by

4 Answers

Best answer
108 votes
108 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 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
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 

2 votes
2 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

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

52 votes
52 votes
3 answers
4
Kathleen asked Sep 14, 2014
11,743 views
The most appropriate matching for the following pairs$$\begin{array}{|ll|ll|}\hline X: & \text{m = malloc(5); m = NULL;} & 1: & \text{using dangling pointers} \\\hline Y...