• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

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

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...

  • 2
1 Solution
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];
>>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.
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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now