Learn how to a build a cloud-first strategyRegister Now


Memory Address and Pointers/Reference

Posted on 2006-04-26
Medium Priority
Last Modified: 2010-04-01

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

Question by:afking8268
  • 2

Expert Comment

ID: 16545208
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];
LVL 86

Accepted Solution

jkr earned 500 total points
ID: 16545218
>>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.
LVL 86

Expert Comment

ID: 16545234
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."

Author Comment

ID: 16545529
Great, thanks!

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question