I COULD NOT GOT THE QUESTION 7 CAN YOU PLEASE ELABORATE HOW TO APPROACH THIS QUESTION?
WHAT IS THE REASON OF COMPILATION ERROR IN QUES 8?
QUES NO.7 AND 8 OF THE MCQs OF POINTERS
the question is:WHAT IS THE VALUE OF (i-1) FOR THE 1ST RUN OF NESTED LOOP?
Hey Aman,
Q7 In the piece of code, arr[ ][ ] is a 2D array and assume that the
contents of the 2D array are already filled up. What is stored in the
variable sum at the end of the code segment?
int arr[3][3];
int i, sum=0; i = 0; while(i<3) {
sum += * ( * (arr+i)+(i++)); }
printf("sum:%d", sum);
In this code *(arr+i)+(i++)
this will results in the addresses where i can be 0,1,2 in the loop and * ( * (arr+i)+(i++))
results in the garbage values at these addresses and then we are simply adding these values using the variable sum.
Q8 Would the following program compile?
#include <iostream>
using namespace std;
int main() {
int a=10, *j; void *k; j=k=&a; j++;
k++; cout<<j<<k; return 0;
}
It will not compile as it will give the compilation error for line: j=k=&a;
because there’s no implicit conversion from void* to type * in C++, so it will give you the error invalid conversion from ‘void*’ to ‘int*’
could you please explain how the sum statement is getting executed?