+ 2
Please explain this output
char s[]="ashu"; cout<<*&*&*s; cout<<*&*&s; output-- a ashu
8 ответов
+ 3
Микола Федосєєв
So, if we want to summarize it, can we simply say that every *& or &* pairs cancel each other out, right?
+ 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
+ 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
*/
+ 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. 😎
+ 1
Yes in general.
0
you said &*s gives address of index 0
but cout<<&*s;
prints ashu
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.
0
thanks Nikhil Dhama