0

I am new to c++ and have a question about the following code: Why does cout in displayString display the actual string and not the address of the string? It's a pointer so its value should be the address right?

#include <iostream>
using namespace std;

class myString
{
    char* stringPtr=NULL;
public:
    myString(char* inputPtr) {
        if(inputPtr!=NULL) {
            stringPtr=new char [strlen(inputPtr)+1];
            stringPtr=inputPtr;
        } else {
            stringPtr=NULL;
        }
    }
    ~myString() {
        if(stringPtr!=NULL) {
            delete [] stringPtr;
        }
    }
    void displayString() {
        cout << stringPtr << endl;
        //WHY DOES THIS DISPLAY THE VALUE THE
        //POINTER POINTS TO AND NOT THE ADDRESS?
    }
};

int main() {
    myString firstString("hello world");
    firstString.displayString();
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118

0 Answers0