Ok, since this code is too long for you to read, I labelled the code. Firstly, i created a pointer called user1 while defining function userpass. The function accepts the memory location of 2 arrays as it's arguments which get transferred to user1 and pass1 pointers. So my question is at the "do" loop, which i marked with another comment "!!!!!!!". When i "cout" user1, it displays the username i entered at the do loop marked with comment "AAAAAAAAA". My question is, why doesnt the cout statement display the memory address of the "user" string from the main(), and why does it display a string instead?
If I have understood your question correctly, then UNBELIEVABLY this is the 3rd time I've come across this question in 2 days!!! Following link may help you understand better:
thanks for the reply, but that just took me to a topic which does not have a certain answer. if you know it, i would like it if you could explain it, because i still don't understand.
cout<<"\n\nEnter USERNAME: "<<user1; // where user1 is a pointer to a char
The operator << interprets it as a CString, rather than a pointer. This is why the entire string is printed out. If you want to output the address, you will have to typecast it as a void pointer.
i actually wanted to output the string itself. it was just that when i was reviewing the code, i realised there was some abnormality. thanks for the help!
> My question is, why doesnt the cout statement display the memory address of the "user" string
> from the main(), and why does it display a string instead?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
int main()
{
constchar cstr[] = "abcd" ;
constvoid* p = cstr ;
std::cout << cstr ;
// overload resolves to: free function which prints a c-style string
// std::ostream& operator<< ( std::ostream&, const char* ) ;
std::operator<< ( std::cout, cstr ) ;
std::cout << p ;
// overload resolves to: member function which prints the value of a pointer
// std::ostream::operator<< ( const void* ) ;
std::cout.operator<< (p) ;
}