The Gateway to Computer Science Excellence
0 votes
165 views

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 Junior (861 points)
retagged by | 165 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

by (101 points)
selected by
0 votes

(2) Option is correct

Ans:bba

by (257 points)

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
50,644 questions
56,505 answers
195,555 comments
101,046 users