The Gateway to Computer Science Excellence
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

in Programming by
retagged by | 266 views

2 Answers

+1 vote
Best answer

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

(2) Option is correct



Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
52,375 questions
60,586 answers
95,409 users