Q2: Suppose that A is an array of int, and that B is declared to be a pointer of type int *. Which of the following is not a valid assignment statement, assuming that both B and the elements of A have been initialized to suitable values:B = A;B = &A[3];B = *(A + 3);B = (A + 3);B = A + *A;
Question
Q2: Suppose that A is an array of int, and that B is declared to be a pointer of type int *. Which of the following is not a valid assignment statement, assuming that both B and the elements of A have been initialized to suitable values:B = A;B = &A[3];B = *(A + 3);B = (A + 3);B = A + *A;
Solution
The statement that is not valid is: B = *(A + 3);
Explanation:
In C, the expression *(A + 3) dereferences the pointer (A + 3), which results in an integer value (since A is an array of integers). However, B is a pointer to an integer, not an integer itself. Therefore, you cannot assign an integer value to a pointer, which makes the statement B = *(A + 3); invalid.
All the other statements are valid. B = A; assigns the address of the first element of the array A to the pointer B. B = &A[3]; assigns the address of the fourth element of the array A to the pointer B. B = (A + 3); assigns the address of the fourth element of the array A to the pointer B. B = A + *A; assigns the address of the element at the index equal to the first element of the array A to the pointer B.
Similar Questions
Q1: Which of the following statements about arrays in C is not true:The first element in the array is at index 0The array name is a pointer constantAll of the elements in the array are of the same typeAn array must always have an associated buddy variable to indicate its sizeAn array can be of any type
Consider the following declaration: int *arr1[20], arr2[10][10];Which of the following expressions will not give compile-time errors if used as left-hand sides of assignment statements in a C program? Choose the most appropriate answer.A. arr1[2]B. arr1[2][3]C. arr2[2]D. arr2[2][3]a.D onlyb.A, B, Dc.B, C, Dd.B, D
Choose the right option for the following code snippet.#include <iostream>using namespace std;int main() { int a;int* x=&a; int y; }Options: Pick one correct answer from belowx is a pointer to a int, y is a inty is a pointer to a int, x is a intBoth x and y are pointers to int typesboth x and y are not pointers
Which of these is an incorrect Statement?a) It is necessary to use new operator to initialize an arrayb) Array can be initialized using comma separated expressions surrounded by curly bracesc) Array can be initialized when they are declaredd) None of the mentioned
Which of the following declaration is illegal?a.int a = 0, b = 1, c = 2; int array[3] = {a, b, c};b.int size = 3; int array[size];c.int size = 3; int array[size] = {1, 2, 3};d.In appropriate
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.