retagged by
1,210 views
0 votes
0 votes

What will be the output:
 

#include <iostream>
using namespace std;

int main()
{
    char *A[] = { "abcx", "dbba", "cccc"};
    char var = *(A+1) - *A+1;
    cout << (*A + var);
}


1. $abba$

2. $bba$

3. $a$

d. none

retagged by

2 Answers

Best answer
1 votes
1 votes

Ans is (2) - bba

char *A[] = { "abcx", "dbba", "cccc"};
char var = *(A+1) - *A+1;
cout << (*A + var);

Put the value of var as it is in cout expression

cout << (*A + *(A+1) - *A + 1);

*A and -*A is cancelled out

We get

cout << *(A+1) + 1;

Now the name of the array holds the address of first element of the array. In this case A will point to "abcx", A+1 will point to "dbba"

*(A+1) will give us the string "dbba"

Now since string literals is also a const char* 

Adding 1 to it will make the pointer move forward by 1 byte, i.e. its gonna point to "bba"

And thus when you'll print it, the ans will be bba

selected by

Related questions

2 votes
2 votes
2 answers
2
atulcse asked Jan 15, 2022
688 views
Consider the following programint find (int n) { int a = 1; for (i = 1; i < = n; i ++) for (j = 1; j < = i; j++) for (k = 1; k <= j, k++) a = a + 1; ...