Memory Address and Pointers/Reference


Suppose I want to get the memory address of the first character in:

char *t = "sample";

I would think that the value of:
cout << &t

and
cout << static_cast< void * >( t )

would be the same since "t" is basically a pointer pointing to the first character of "sample". However, when I run it, I get different values... can someone help me understand? I know I'm missing something...

Thanks
afking8268Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dariusz DziaraProgrammerCommented:
cout << &t

In this case  you will print address of variable "t"

cout << static_cast< void * >( t )

In that you will print address "t"

To print string you should use:

cout << t;

To print thr first character:

cout << t[0];
jkrCommented:
>>Suppose I want to get the memory address of the first character in:
>>char *t = "sample";

That is simply the value of 't' itself, nothing else. '&t' is the address where the variable 't' is stored, which in turn points to the location of "sample". These have to be different values.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
BTW, there seems to be a misconception - '&' is the adress operator:

"The address-of operator (&) gives the address of its operand. The operand of the address-of operator can be either a function designator or an l-value that designates an object that is not a bit field and is not declared with the register storage-class specifier.

The result of the address operation is a pointer to the operand. The type addressed by the pointer is the type of the operand."
afking8268Author Commented:
Great, thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.