+ 2

Please explain this output

char s[]="ashu"; cout<<*&*&*s; cout<<*&*&s; output-- a ashu

7th Jul 2018, 6:42 AM
Apoorv Jain
Apoorv Jain - avatar
8 Answers
+ 3
Микола Федосєєв So, if we want to summarize it, can we simply say that every *& or &* pairs cancel each other out, right?
13th Jul 2018, 12:14 PM
Babak
Babak - avatar
+ 2
here & operator gives the address *s will print the value at location pointing by s *&*&*s; you should know , name of array is a pointer to the base address of that array *s -> give value at index 0 &*s -> address of index 0 *&*s -> value at address (&*s) which is of index 0 &*&*s same as &*s *&*&*s same as *&*s => prints value at index 0=> a *&*&s; as in cpp cout doesn't require specifier to determine the type of variable, it treat s as a string type and cout<<s; will print the value of while array. &s will give the address of s *& s the value of s &*&s the address of s *&*&s again value of s ==> ashu
7th Jul 2018, 6:56 AM
Nikhil Dhama
Nikhil Dhama - avatar
+ 2
#include <iostream> using namespace std; int main() { char t[] = "str"; cout << t << endl; cout << *t << endl; cout << &*t << endl; cout << *&*t << endl; cout << &*&*t << endl; cout << *&*&*t << endl; cout << &*&*&*t << endl; cout << *&*&*&*t << endl; return 0; } /* output: str s str s str s str s */
13th Jul 2018, 10:26 AM
Микола Федосєєв
Микола Федосєєв - avatar
+ 2
hi, bro. take a look: char s [] = "ashu"; // declares an array of char and initializes it by passing the value "ashu"; cout << *&*&*s; // the 1st cout statement displays the content of the 1st char array position ('a'), at index 0; cout << *&*&s; // the 2nd cout statement displays the content (value) of the whole array of char ("ashu"). P.S: for more info, I strongly suggest the Sololearn's C++ Course (see the pointers lesson, about the address of operator (&) n the value of operator (*)). greetings from Brazil. 😎
13th Jul 2018, 12:33 PM
Davis Aguiar
Davis Aguiar - avatar
+ 1
Yes in general.
13th Jul 2018, 12:23 PM
Микола Федосєєв
Микола Федосєєв - avatar
0
you said &*s gives address of index 0 but cout<<&*s; prints ashu
7th Jul 2018, 7:02 AM
Apoorv Jain
Apoorv Jain - avatar
0
Apoorv Jain sorry my bad for any other type(int, float..) it should give the address of index 0. but in case of character type array or string, it some kind referring to the whole string.
7th Jul 2018, 7:26 AM
Nikhil Dhama
Nikhil Dhama - avatar
0
7th Jul 2018, 7:43 AM
Apoorv Jain
Apoorv Jain - avatar