Memory Address and Pointers/Reference

Posted on 2006-04-26
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
    LVL 8

    Expert Comment

    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

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

    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

    Great, thanks!

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now